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

提交代码

分情况讨论。其实代码可以写的更加简洁的,很多过程是重复的。

 #include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
long long gcd(long long a,long long b){//不能保证返回的符号,但能保证返回的绝对值大小
if(b==){
if(a<){
a=-a;
}
return a;
}
return gcd(b,a%b);
}
long long com;
void output(long long fz1,long long fm1){
if(fz1==){
printf("");
return;
}
com=gcd(fz1,fm1);
fz1/=com;
fm1/=com; //cout<<fz1<<" "<<fm1<<" "<<com<<endl; if(fz1%fm1==){
printf("%lld",fz1/fm1);
}
else{
if(fz1/fm1){
printf("%lld ",fz1/fm1);
if(fz1<){
fz1=-fz1;
}
}
printf("%lld/%lld",fz1-fz1/fm1*fm1,fm1);
}
}
void add(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" + ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
com=gcd(fm1,fm2);
fz2*=fm1/com;
fz1*=fm2/com;
fm1*=fm2/com; //cout<<fz1<<" "<<fm1<<" "<<fz2<<" "<<fm2<<endl; printf(" = ");
fz1+=fz2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void sub(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" - ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
com=gcd(fm1,fm2);
fz2*=fm1/com;
fz1*=fm2/com;
fm1*=fm2/com;
printf(" = ");
fz1-=fz2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void mul(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" * ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
printf(" = ");
fz1*=fz2;
fm1*=fm2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void quo(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" / ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
printf(" = ");
fz1*=fm2;
fm1*=fz2;
if(fm1==){
printf("Inf");
return;
}
if(fm1<){
fm1=-fm1;
fz1=-fz1;
}
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
long long fz1,fm1,inter1,fz2,fm2,inter2;
scanf("%lld/%lld %lld/%lld",&fz1,&fm1,&fz2,&fm2); //cout<<fm2<<endl; com=gcd(fz1,fm1);
fz1/=com;
fm1/=com;
com=gcd(fz2,fm2); //cout<<com<<endl;
//cout<<fm2<<endl;
fz2/=com;
fm2/=com;
//cout<<fm2<<endl;
//计算
//cout<<fz1<<" "<<fm1<<" "<<fz2<<" "<<fm2<<endl;
add(fz1,fm1,fz2,fm2);
printf("\n");
sub(fz1,fm1,fz2,fm2);
printf("\n");
mul(fz1,fm1,fz2,fm2);
printf("\n");
quo(fz1,fm1,fz2,fm2);
printf("\n");
return ;
}

pat1088. Rational Arithmetic (20)的更多相关文章

  1. PAT1088:Rational Arithmetic

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

  2. PAT Rational Arithmetic (20)

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

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

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

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

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

  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)

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

  8. PAT_A1088#Rational Arithmetic

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

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

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

随机推荐

  1. 文本PDG文件名构成

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2008.08.03 文本PDG的构成规则为:<前缀><起始页号>_<页数>.pdg 前缀 ...

  2. .Net Core下基于NPOI对Excel、Word操作封装

    本库进行了重写,如果需要请转移到下文查看: https://www.cnblogs.com/holdengong/p/10889780.html 框架与依赖 框架:.NET Standard 2.0 ...

  3. Repeater控件的分隔线

    在Repeater控件中,很容易实现在行与行之间使用分隔线.因为此控件有内置的SeparatorTemplate模版.举个例子吧: 运行时,可以看到效果: 说句实在的话,Insus.NET做一条水平线 ...

  4. 高效 MacBook 工作环境配置

    转自:https://mp.weixin.qq.com/s/sloc6HgKcosXtWcbMB_5hA 工欲善其事,必先利其器,工具永远都是用来解决问题的,没必要为了工具而工具,一切工具都是为了能快 ...

  5. sap程序下载

    有时需要下载sap 中的程序   一屏一屏的拷贝非常麻烦 这里是在网上找的的一个下载工具   实际就是一个sap程序   建立好sap程序 把代码拷贝进去 在参数处输入程序名字  和下载位置  F8运 ...

  6. c++构造函数问题,初始化和赋值问题

    默认构造函数(就是没有参数的构造函数) The Default ConstructorThe default constructor is the constructor used to create ...

  7. Unity 移动 和 旋转 [小结]

    [移动]  Position: 说明: 直接修改位置数据  Translate: 说明: [匀速]朝着一个方向,一直移动. (dir * speed 可以控制速度)适合键盘控制物体上下左右运动 函数: ...

  8. ssh免密登录linux服务器

    Ssh免密登录 sshd服务 sshd简介: SSH 密钥为登录 Linux 服务器提供了更好且安全的机制.运行 ssh-keygen 后,将会生成公私密钥对.你可以将公钥放置到任意服务器,从持有私钥 ...

  9. python 中文路径问题

    Python直接读取中文路径的文件时失败,可做如下处理: inpath = 'D:/work/yuanxx/在线导航/驾车导航/walk_log/20130619_172355.txt' uipath ...

  10. js-eval运算符

    js中使用eval运算符需要注意—— eval()只有一个参数 传入的参数是字符串时,才会去解析执行:否则,将直接返回这个参数 作用域与调用它的变量作用域保持一致 返回字符串中最后一个表达式或语句的值 ...