1088 Rational Arithmetic(20 分)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result. Notice that all the rational numbers must be in their simplest form k a/b, where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output Inf as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:

2/3 -4/2

Sample Output 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

Sample Input 2:

5/3 0/6

Sample Output 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

题目大意:给出两个分数,并给出其加减乘除的结果表示,在除法时,如果分子为0,那么输出Inf。

//虽然从来没做过这样的题目,但是感觉还是比较简单的。

#include <stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int main() {
long int a[],b[];
long int re1[],re2[],re3[],re[];
scanf("%ld/%ld %ld/%ld",a[],a[],b[],b[]);
//那么这个就是求最小公倍数,最大公因数的问题了。
long int ming=max(a[],b[]);
long long x=a[]*b[];
for(int i=ming;i<=x;i++){
if(i%a[]==&&i%b[]==){
ming=i;break;
}
}
a[]=a[]/a[];
b[]=b[]/b[];
a[]=a[]*ming/a[];
b[]=b[]*ming/b[];
a[]=ming;
b[]=ming;
re1[]
//需要存好多种形式的结果啊! return ;
}

//自己写着写着就写不下去了。没有底。各种细节,感觉控制不了。

代码来自:https://www.liuchuo.net/archives/1906

#include <iostream>
#include<stdio.h>
using namespace std;
long long int a, b, c, d;//使用这种方法需要数据的范围很大。 long long int gcd(long long int t1, long long int t2) {
return t2 == ? t1 : gcd(t2, t1 % t2);
} void func(long long int m, long long int n) {
int flag1 = ;
int flag2 = ;
if (n == ) {//分母肯定是不能为0得,如果有1/3 0/1这样的输入,在加法中,分母会变成3而不是0.
cout << "Inf";
return ;
}
if (m == ) {
cout << ;
return ;
} if (m < ) {
m = - m;
flag1 = ;
}
if (n < ) {
n = - n;
flag2 = ;
}
int flag = ;
if (flag1 == && flag2 == ) {
flag = ;
} else if (flag1 == || flag2 == ) {
flag = ;//这个主要是来确定整个结果包括分子和分母的符号
}
if (m == n) {
if (flag == )
cout << "(-1)";//计算结果为负值,需要加括号
else
cout << "";//计算结果为正值,不需要加。
return;
} long long int x = m % n;//因为m不可能=0了,之前已经判断过了,所以此处
//如果x为0,那么肯定就是没有余数。
long long int y = m / n;
if (x == ) {
if (flag == )
cout << y;
else
cout << "(-" << y << ")";
return ;
} else {
long long int t1 = m - y * n;
long long int t2 = n;
long long int t = gcd(t1, t2);
t1 = t1 / t;
t2 = t2 / t;
if (flag == ) {
cout << "(-";
if (y != )//假分数
cout << y << " " << t1 << "/" << t2;
else
cout << t1 << "/" << t2;
cout << ")";
} else {//真分数
if (y != )
cout << y << " " << t1 << "/" << t2;
else
cout << t1 << "/" << t2;
}
}
} void add() {
long long int m, n;//还没见过这个数据类型
m = a * d + b * c;//直接这样得出分子,厉害。
n = b * d;//分母。
func(a, b);//处理方式都是一样的。
cout << " + ";
func(c, d);
cout << " = ";
func(m, n);
cout << endl;
} void min() {
long long int m, n;
m = a * d - b * c;
n = b * d;
func(a, b);
cout << " - ";
func(c, d);
cout << " = ";
func(m, n);
cout << endl;
} void multi() {
long long int m, n;
m = a * c;
n = b * d;
func(a, b);
cout << " * ";
func(c, d);
cout << " = ";
func(m, n);
cout << endl;
} void div() {
long long int m, n;
m = a * d;
n = b * c;
func(a, b);
cout << " / ";
func(c, d);
cout << " = ";
func(m, n);
cout << endl;
} int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
add();
min();
multi();
div();
return ;
}

//学习了!各种细节考虑的都很好。

1.对每一个数都有一个函数进行处理,十分简洁。

2.由于数可能会非常大,所以使用了long long int这种数据类型

3.顺便复习了,如何求两个数得最大公因数。!

PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]的更多相关文章

  1. PAT 1088. Rational Arithmetic

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...

  2. 1088 Rational Arithmetic

    题意: 给出两个分式(a1/b1 a2/b2),分子.分母的范围为int型,且确保分母不为0.计算两个分数的加减乘除,结果化为最简的形式,即"k a/b",其中若除数为0的话,输出 ...

  3. PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

    输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...

  4. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

  5. PAT (Advanced Level) 1088. Rational Arithmetic (20)

    简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...

  6. 【PAT甲级】1088 Rational Arithmetic (20 分)

    题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...

  7. 1088 Rational Arithmetic(20 分)

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...

  8. 1088. Rational Arithmetic (20)

    1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...

  9. PAT_A1088#Rational Arithmetic

    Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...

随机推荐

  1. Windows "计划任务"功能设置闹钟~

    相信很多人和我一样在使用电脑时都会遇到这样一个麻烦:不知道如何在windows 中设置一个闹铃.当我们在“开始”菜单的所有程序中找了一遍又一遍,甚至使用Everything.exe做全盘的搜索,都没有 ...

  2. Effective C++ Item 18 Make interfaces easy to use correctly and hard to use incorrectly

    1. A good API will provide easy to use interfaces but also provide hard to miss-use interfaces. Usua ...

  3. Objective-C语法之KVC使用

    转自:http://www.cnblogs.com/stoic/archive/2012/07/20/2601315.html 除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding ...

  4. linux中的目录和文件的统计

    ls 目录 | wc -l find  ./   -type   d   |   wc   -l      //查找目录个数 find   ./   -type   f   |   wc   -l   ...

  5. 用代码走进Ftp

    因为最近做一个关于集中采集的ftp改造开发.所以研究了哈ftp的开发. 一个简单常用的连接ftp的命令:ftp 主机ip 下面贴出我自己的ftp的demo. 1.FtpUtil工具类 import j ...

  6. mybatis 返回值类型是Map

    <select id="selectByMemberKey" resultType="java.util.HashMap"> SELECT memb ...

  7. PHP虚拟主机的配置

    今天配置了一下虚拟目录,以下是我的配置方法. 1  编辑httpd.conf,查找Include conf/extra/httpd-vhosts.conf,把前面注释符号“#”删掉. 2  编辑htt ...

  8. <转>SVM实现之SMO算法

    转自http://blog.csdn.net/zouxy09/article/details/17292011 终于到SVM的实现部分了.那么神奇和有效的东西还得回归到实现才可以展示其强大的功力.SV ...

  9. 【LNMP】 fileinfo扩展安装

    1  查看服务器php版本 : php -v 2  进入目录 , 解压相对应的PHP 版本压缩包, cd /lnmp1./srctar zxvf php-7.0.tar.gz 3  输入以下命令 cd ...

  10. Android 命令行打包和签名

    使用命令行方式进行签名需要JDK中的两个命令行工具:keytool.exe和jarsigner.exe.可按如下两步对apk文件进行签名: 1. # keytool -genkey -v -keyst ...