PAT1088:Rational Arithmetic
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 思路 分数的四则运算,难点主要是在输出数据的格式化(参见代码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的更多相关文章
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- A1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- 1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
- PAT 1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
随机推荐
- GCC内联函数:__builtin_types_compatible_p
#if 0 - Built-in Function: int __builtin_types_compatible_p (type1, type2) You can use the built-in ...
- Java集合之TreeSet
TreeSet是一个有序的集合,它的作用是提供有序的Set集合.它继承了AbstractSet抽象类,实现了NavigableSet<E>,Cloneable,Serializable接口 ...
- Linux 打开句柄限制的调整
Linux 打开句柄限制的调整 参考文章: Linux---进程句柄限制总结(http://blog.csdn.net/jhcsdb/article/details/32338953) !! 本文内容 ...
- 使用JS取得焦点(focus)元素
原文链接: Get the Focused Element with JavaScript 原文日期: 2014年3月19日 翻译日期: 2014年3月21日 翻译人员: 铁锚 对于良好的用户体验来说 ...
- ubuntu14.04系统中virtualbox安装Oracle VM VirtualBox Extension Pack包
ubuntu14.04系统中virtualbox默认不支持usb设备,需要安装Oracle VM VirtualBox Extension Pack才行,但必须安装以下版本才可以安装成功: Oracl ...
- Media Player Classic - HC 源代码分析 3:核心类 (CMainFrame)(2)
===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...
- VS2010中使用Jquery调用Wcf服务读取数据库记录
VS2010中使用Jquery调用Wcf服务读取数据库记录 开发环境:Window Servere 2008 +SQL SERVE 2008 R2+ IIS7 +VS2010+Jquery1.3.2 ...
- Shell Scripts - 条件语句,case语句,function功能
修改之前的代码 1.判断 $1 是否为 hello,如果是的话,就显示 "Hello, how are you ?": 2.如果没有加任何参数,就提示使用者必须要使用的参数 ...
- Slop One 算法
Slope One 算法是由 Daniel Lemire 教授在 2005 年提出的一个 Item-Based 推荐算法. Slope One 算法试图同时满足这样的的 5 个目标: 易于实现和维护: ...
- 【Android 应用开发】对Android体系结构的理解--后续会补充
1.最底层_硬件 任何Android设备最底层的硬件包括 显示屏, wifi ,存储设备 等. Android最底层的硬件会根据需要进行裁剪,选择自己需要的硬件. 2.Linux内核层 该层主要对硬件 ...