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. Linux管道编程实例

    /*管道 可以把管道想象为两个实体之间的单向连接器.注意,管道是半双工的, 如果需要全双工通讯,应该转而考虑套接字. 匿名管道又称管道,提供了一个进程与它的兄弟进程通讯的方法,只存在于父进程中: 命名 ...

  2. android开发之http协议

    http协议学习系列 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web C ...

  3. 通过服务修改widgetUI

    public static void updateAppWidget(Context context, String displayMsg) { AppWidgetManager appWidgetM ...

  4. ANDROID 中设计模式的采用--创建型模式

     所谓模式就是在某一情景下解决某个问题的固定解决方案. 所有的创建型模式都是用作对象的创建或实例化的解决方案. 1 简单工厂模式 创建对象的最简单方法是使用new来创建一个对象,如果只创建一种固定 ...

  5. 在 Vim 中设置 Tab 为4个空格

    缩进用 tab 制表符还是空格,这不是个问题,就像 python 用四个空格来缩进一样,这是要看个人喜好的.在 Vim 中可以很方便的根据不同的文件类型来设置使用 tab 制表符或者空格,还可以设置长 ...

  6. glib-dbus 在ubuntu9.10 和 ubuntu10.04 上安装环境的搭建

    dbus-glib 安装环境搭建 安装 dbus apt-get install dbus 安装 d-feet ,用于查看 session bus 和 system bus apt-get insta ...

  7. 安卓学习笔记--已root设备应用请求root权限

    网上查到的方法 Process process = null; DataOutputStream os = null; try { String cmd = "chmod 777 " ...

  8. 复位windows网络参数的方法

    使用电脑的时候,经常会遇到网络相关的问题,以前读大学的时候就知道怎么解决,就是下面这个方案. 开始-全部程序-附件-命令提示符-右键-以管理员身份运行出来一个黑底白字的窗口,在里面输入: netsh ...

  9. svn 不能添加.a文件

    1.打开终端输入    open ~/.subversion/ 2.双击打开config文件 3.修改如下两行 # global-ignores = *.o *.lo *.la *.al .libs ...

  10. jquery-取消冒泡

    1.通过返回false来取消默认的行为并阻止事件起泡. jQuery 代码: $("form").bind( "submit", function() { re ...