Rational Arithmetic (20)
来源:互联网 发布:小智用什么软件刷点卷 编辑:程序博客网 时间:2024/06/13 21:21
题目描述
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.
输入描述:
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.
输出描述:
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.
输入例子:
5/3 0/6
输出例子:
1 2/3 + 0 = 1 2/31 2/3 - 0 = 1 2/31 2/3 * 0 = 01 2/3 / 0 = Inf
我的代码:
(注:此代码只在牛客网上可以通过)
#include<iostream>using namespace std;int gys(int x,int y){ if(y%x==0) return x; else return gys(y%x,x);}int main(){ int i,n,m,x,y,a,b,c,d,flag=1,flag1=1,flag2=1;int x1,y1,a1,b1,flag3=1,x2,y2,a2,b2,flag4=1;int x3,y3,a3,b3,flag5=1,x4,y4,a4,b4,flag6=1; scanf("%d/%d %d/%d",&n,&m,&x,&y); if(m==0 || y==0) return 0; if(x==0) flag=0; a=n/m,b=n%m,c=x/y,d=x%y; if(a<0) { a=-a; flag1=0; } if(b<0) { b=-b; flag1=0; } if(c<0) { c=-c; flag2=0; } if(d<0) { d=-d; flag2=0; } for(i=0;i<4;i++) { if(flag1==0) printf("(-"); if(a!=0) cout<<a; if(b!=0) {int e=gys(b,m); if(a!=0) cout<<" "<<b/e<<'/'<<m/e; else cout<<b/e<<'/'<<m/e; } else { if(a==0) cout<<0; } if(flag1==0) cout<<')'; if(i==0) cout<<" "<<'+'<<" "; else if(i==1) cout<<" "<<'-'<<" "; else if(i==2) cout<<" "<<'*'<<" "; else cout<<" "<<'/'<<" "; if(flag2==0) printf("(-"); if(c!=0) cout<<c; if(d!=0) {int f=gys(d,y); if(c!=0) cout<<" "<<d/f<<'/'<<y/f; else cout<<d/f<<'/'<<y/f; } else { if(c==0) cout<<0; } if(flag2==0) cout<<')'; cout<<" "<<'='<<" "; if(i==0) { x1=m*y,y1=n*y+m*x,a1=y1/x1,b1=y1%x1; if(x1<0) { flag3=0; x1=-x1; } if(a1<0) { flag3=0; a1=-a1; } if(b1<0) { flag3=0; b1=-b1; } if(flag3==0) printf("(-"); if(a1!=0) cout<<a1; if(b1!=0) {int e1=gys(b1,x1); if(a1!=0) cout<<" "<<b1/e1<<'/'<<x1/e1; else cout<<b1/e1<<'/'<<x1/e1; } else { if(a1==0) cout<<0; } if(flag3==0) cout<<')'; } else if(i==1) { x2=m*y,y2=n*y-m*x,a2=y2/x2,b2=y2%x2; if(x2<0) { flag4=0; x2=-x2; } if(a2<0) { flag4=0; a2=-a2; } if(b2<0) { flag4=0; b2=-b2; } if(flag4==0) printf("(-"); if(a2!=0) cout<<a2; if(b2!=0) {int e2=gys(b2,x2); if(a2!=0) cout<<" "<<b2/e2<<'/'<<x2/e2; else cout<<b2/e2<<'/'<<x2/e2; } else { if(a2==0) cout<<0; } if(flag4==0) cout<<')'; } else if(i==2) { x3=m*y,y3=n*x,a3=y3/x3,b3=y3%x3; if(x3<0) { x3=-x3; flag5=0; } if(a3<0) { a3=-a3; flag5=0; } if(b3<0) { b3=-b3; flag5=0; } if(flag5==0) printf("(-"); if(a3!=0) cout<<a3; if(b3!=0) {int e3=gys(b3,x3); if(a3!=0) cout<<" "<<b3/e3<<'/'<<x3/e3; else cout<<b3/e3<<'/'<<x3/e3; } else { if(a3==0) cout<<0; } if(flag5==0) cout<<')'; } else { if(flag==0) cout<<"Inf";else if(a==0 && b==0) cout<<0; else { x4=m*x,y4=n*y,a4=y4/x4,b4=y4%x4; if(x4<0) { flag6=0; x4=-x4; } if(a4<0) { flag6=0; a4=-a4; } if(b4<0) { flag6=0; b4=-b4; } if(flag6==0) printf("(-"); if(a4!=0) cout<<a4; if(b4!=0) {int e4=gys(b4,x4); if(a4!=0) cout<<" "<<b4/e4<<'/'<<x4/e4; else cout<<b4/e4<<'/'<<x4/e4; } else { if(a4==0) cout<<0; } if(flag6==0) cout<<')'; } } puts(""); } return 0;}
阅读全文
0 0
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- PAT Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 面试题-面向对象篇
- 单例模式VS静态方法
- c#在字符中四位一组添加\u构成Unicode编码
- 移动端点击某个按钮触发的时候,出现点击状态的方法
- 使用阿里云、腾讯云服务器挖比特币的方法(ubuntu)
- Rational Arithmetic (20)
- Bootstrap深度解析
- hdu 1800 Flying to the Mars 暴力解决
- 自动化提取关键字
- ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法
- 【局部图像描述子】Harris角点检测器
- 判断不可能的出栈序列
- poj 1014
- codeforces 363B Fence