Source:

PAT A1088 Rational Arithmetic (20 分)

Description:

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

Keys:

Code:

 /*
Data: 2019-07-05 20:10:35
Problem: PAT_A1088#Rational Arithmetic
AC: 21:04 题目大意:
分数的加减乘除
*/
#include<cstdio>
#include<algorithm>
using namespace std;
struct fr
{
long long up,down;
}s1,s2; long long GCD(long long a, long long b)
{
return !b ? a : GCD(b,a%b);
} fr Reduct(fr r)
{
if(r.down < )
{
r.up = -r.up;
r.down = -r.down;
}
if(r.up==)
r.down=;
else
{
int gcd = GCD(abs(r.up),abs(r.down));
r.up /= gcd;
r.down /= gcd;
}
return r;
} fr Add(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down+f1.down*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Dif(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down-f1.down*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Pro(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.up;
r.down = f1.down*f2.down;
return Reduct(r);
} fr Quo(fr f1, fr f2)
{
fr r;
r.up = f1.up*f2.down;
r.down = f1.down*f2.up;
return Reduct(r);
} void Show(fr r)
{
r = Reduct(r);
if(r.up < )
printf("(");
if(r.down == )
printf("%lld", r.up);
else if(abs(r.up) > r.down)
printf("%lld %lld/%lld", r.up/r.down,abs(r.up)%r.down,r.down);
else
printf("%lld/%lld", r.up,r.down);
if(r.up < )
printf(")");
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%lld/%lld %lld/%lld", &s1.up,&s1.down,&s2.up,&s2.down);
char op[] = {'+','-','*','/'};
for(int i=; i<; i++)
{
Show(s1);printf(" %c ", op[i]);Show(s2);printf(" = ");
if(i==) Show(Add(s1,s2));
else if(i==) Show(Dif(s1,s2));
else if(i==) Show(Pro(s1,s2));
else
{
if(s2.up==) printf("Inf");
else Show(Quo(s1,s2));
}
printf("\n");
} return ;
}

PAT_A1088#Rational Arithmetic的更多相关文章

  1. PAT1088:Rational Arithmetic

    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. pat1088. Rational Arithmetic (20)

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

  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. java com.db4o 类

    <!--juan_zhai--> <dependency> <groupId>com.db4o</groupId> <artifactId> ...

  2. centos svn 安装

    #检查是否安装了低版本的SVN [root@zck /]# rpm -qa subversion #卸载旧版本SVN [root@zck modules]# yum remove subversion ...

  3. Opengl 之 窗口初体验 ------ By YDD的铁皮锅

    大二的时候开始想着做游戏,因为学校的课程实在是无聊就想着做些有意义的事情.毕竟学了编程这一行就得做些实事,于是就在网上搜了一下图形编程,偶然的了解到了Opengl (同时还有Windows上的Dire ...

  4. DNS域名解析服务以及Bind服务程序

    一般来讲域名比IP地址更加的有含义.也更容易记住,所以通常用户更习惯输入域名来访问网络中的资源,但是计算机主机在互联网中只能通过IP识别对方主机,那么就需要DNS域名解析服务了. DNS域名解析服务( ...

  5. 大型项目必备IPC之Binder机制原理(一)

    阿里P7Android高级架构进阶视频免费学习请点击:https://space.bilibili.com/474380680 摘要 Binder是Android系统进程间通信(IPC)方式之一.Li ...

  6. Linux/x86-64 - setuid(0) & chmod ("/etc/passwd", 0777) & exit(0) - 63 byes

    /* Title: Linux/x86-64 - setuid(0) & chmod ("/etc/passwd", 0777) & exit(0) - 63 by ...

  7. Elasticsearch添加Shield后TransportClient如何连接?

    Elasticsearch添加Shield后TransportClient如何连接? 时间 2015-12-28 10:24:01  旁门左道 原文  http://log.medcl.net/ite ...

  8. 【LeetCode】贪心

    [452] Minimum Number of Arrows to Burst Balloons [Medium] 给一堆线段,使用最少的arrow,穿过所有的线段.陈题,第一条线段的终点. Inpu ...

  9. 初撩RESTful

    1. 什么是RESTful? 一种软件架构风格,设计风格,用于客户端和服务端交互类的架构. 一组架构约束条件和原则 2. 什么是RESTful架构? 客户端通过http动词(get/post等)对服务 ...

  10. 浅谈异步上传插件 jquery-file-upload插件

    当我们需要异步上传文件的时候,我们倾向于在网上查找相关的JQuery插件,jquery-file-upload就是我们经常看到的,但是他的主页是英文的,对于我们这些英语比较差的同学来说,简直就是... ...