1088. Rational Arithmetic (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 思路 分数的四则运算,难点主要是在输出数据的格式化(参见代码stringFormat函数)上,比较考验耐心和逻辑,仔细就行。 代码
#include<iostream>
using namespace std;
typedef long long ll; ll a,b,c,d,gcdvalue; ll gcd(ll a,ll b)
{
return b == 0?abs(a):gcd(b,a % b);
} string stringFormat(ll a,ll b)
{
string res = "";
gcdvalue = gcd(a,b);
a /= gcdvalue;
b /= gcdvalue;
if(a * b < 0)
res += "(-";
ll c = abs(a), d = abs(b);
if(c/d == 0)
{
if( c % d == 0)
res += "0";
else
res += to_string(c) + "/" +to_string(d);
}
else
{
res += to_string(c/d);
if(c % d != 0)
{
res += " " + to_string(c % d) + "/" +to_string(d);
}
}
if(a * b < 0)
res += ")";
return res;
} int main()
{
scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
ll sum1,sum2,sub1,sub2,mul1,mul2,div1,div2;
//sum
sum1 = a * d + b * c;
sum2 = b * d;
sub1 = a * d - b * c;
sub2 = b * d;
mul1 = a * c;
mul2 = b * d;
div1 = a * d;
div2 = b * c;
string a1 = stringFormat(a,b);
string a2 = stringFormat(c,d);
cout << a1 << " + " << a2 <<" = " << stringFormat(sum1,sum2) << endl;
cout << a1 << " - " << a2 <<" = " << stringFormat(sub1,sub2) << endl;
cout << a1 << " * " << a2 <<" = " << stringFormat(mul1,mul2) << endl;
if(a2 != "0")
cout << a1 << " / " << a2 <<" = " << stringFormat(div1,div2) << endl;
else
cout << a1 << " / " << a2 <<" = " << "Inf" << endl;
}

  

PAT1088:Rational Arithmetic的更多相关文章

  1. pat1088. Rational Arithmetic (20)

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

  2. PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]

    1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...

  3. PAT_A1088#Rational Arithmetic

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

  4. A1088. Rational Arithmetic

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

  5. 1088 Rational Arithmetic(20 分)

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

  6. PAT Rational Arithmetic (20)

    题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...

  7. PAT 1088. Rational Arithmetic

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

  8. PAT甲级——A1088 Rational Arithmetic

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

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

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

随机推荐

  1. JavaScript进阶(四)js字符串转换成数字的三种方法

    js字符串转换成数字的三种方法 在js读取文本框或者其它表单数据的时候获得的值是字符串类型的,例如两个文本框a和b,如果获得a的value值为11,b的value值为9 ,那么a.value要小于b. ...

  2. 好看的dialog,sweet Alert Dialog 导入Android Studio

    系统自带的dialog实在是丑到无法忍受.所以找到了一款比较好的第三方dialog. github 地址如下:https://github.com/pedant/sweet-alert-dialog ...

  3. InfiniDB 修改一行的效率?

    InfiniDB引擎的DML速度比较慢,无论设置自动提交开关为关闭或开启,插入性能都很糟糕,但更新和删除的效率还可以,并且不支持truncate表操作. 删,改 效率高 插入,效率低(测试,在数据量稍 ...

  4. 测试access函数

    测试程序: 测试结果: chown root access.out 将用户ID改为root chmod u+s access.out 打开 set-user-ID位

  5. 增加AP INVOICE 行&分配行

    -- 增加行 DECLARE v_row_id VARCHAR2(1000); v_line_number number; g_user_id CONSTANT NUMBER := fnd_globa ...

  6. RPi Kernel Compilation

    Overview This page explains how to rebuild the kernel image for the RPi. There are two possible rout ...

  7. 在AndroidManifest.xml文件中设置Android程序的启动界面方法

    从网上搜集了一堆的Android代码,比如Android的Login程序和Android的Helloworld程序,但是却总不能正确运行一个正确的程序,郁闷了很久,终于在一次一次的测试后成功的在And ...

  8. rails使用QQ邮箱发送邮件蛋疼的经历

    以前本猫在blog中写过使用ruby发送邮件的博文,其中使用了163和qq的邮箱发送邮件都可以发送成功.但是现在使用rails的发送邮件功能,使用的是qq的邮件服务器发送,死活不可以!要不就是认证失败 ...

  9. 64位ubuntu安装N64模拟器mupen64

    我们知道在windows平台下模拟器多如牛毛,N64的模拟器也不例外.而linux下对于想玩N64的童鞋们有啥好办法呢?我通过度娘找到一款开源的N64模拟器mupen64,其官方网址为:http:// ...

  10. window配置mongodb集群(副本集)

    参数解释: dbpath:数据存放目录 logpath:日志存放路径 pidfilepath:进程文件,有利于关闭服务 logappend:以追加的方式记录日志(boolean值) replSet:副 ...