Rational Arithmetic
来源:互联网 发布:nginx获取header信息 编辑:程序博客网 时间:2024/05/18 15:27
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/2Sample Output 1:
2/3 + (-2) = (-1 1/3)2/3 - (-2) = 2 2/32/3 * (-2) = (-1 1/3)2/3 / (-2) = (-1/3)Sample Input 2:
5/3 0/6Sample Output 2:
1 2/3 + 0 = 1 2/31 2/3 - 0 = 1 2/31 2/3 * 0 = 01 2/3 / 0 = Inf
提交代码
#include<stdio.h>#include<algorithm>using namespace std;typedef long long ll;ll gcd(ll a,ll b){if(b==0) return a;else return gcd(b,a%b);}struct Fraction{ll up,down;};Fraction reduction(Fraction result){if(result.down<0){result.down =-result.down;result.up=-result.up;}if(result.up==0){result.down=1;}else{ll d=gcd(abs(result.up),abs(result.down));result.up=result.up/d;result.down=result.down/d;}return result;}Fraction add(Fraction a,Fraction b){Fraction result;result.up=a.up*b.down+a.down*b.up;result.down=a.down*b.down;return reduction(result);}Fraction minu(Fraction a,Fraction b){Fraction result;result.up=a.up*b.down-a.down*b.up;result.down=a.down*b.down;return reduction(result);}Fraction multi(Fraction a,Fraction b){Fraction result;result.up=a.up*b.up;result.down=a.down*b.down;return reduction(result);}Fraction divide(Fraction a,Fraction b){Fraction result;result.up=a.up*b.down;result.down=a.down*b.up;return reduction(result);}void prin(Fraction a){a=reduction(a);if(a.up<0) printf("(");if(a.down==1){printf("%lld",a.up);}else if(abs(a.up)>abs(a.down)){printf("%lld %lld/%lld",a.up/a.down,abs(a.up)%a.down,a.down);}else{printf("%lld/%lld",a.up,a.down);}if(a.up<0) printf(")");}Fraction choose(Fraction a,Fraction b,char c){if(c=='+'){ return add(a,b);}else if(c=='-'){return minu(a,b);}else if(c=='*'){return multi(a,b); } } int main(){Fraction temp1,temp2;scanf("%lld/%lld %lld/%lld",&temp1.up,&temp1.down,&temp2.up,&temp2.down);temp1=reduction(temp1);temp2=reduction(temp2);char str[4]={'+','-','*','/'};for(int i=0;i<3;i++){prin(temp1);printf(" %c ",str[i]);prin(temp2);printf(" = ");prin(choose(temp1,temp2,str[i]));printf("\n");}prin(temp1);printf(" %c ",str[3]);prin(temp2);printf(" = ");if(temp2.up==0) printf("Inf");else prin(divide(temp1,temp2));printf("\n");return 0;}
- Rational Arithmetic
- 1088. Rational Arithmetic (20)
- PAT 1088. Rational Arithmetic
- 1088. Rational Arithmetic (20)
- 1088.Rational Arithmetic
- 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)
- Caused by: java.lang.ClassNotFoundException,还有关于包冲突一些问题
- 数据结构实验之链表三:链表的逆置
- js学习笔记
- 如何正确在集合遍历的时候删除多个元素
- PHPCMS V9实现单页面做频道时也可编辑内容
- Rational Arithmetic
- Android Studio模拟器打开developer options
- Spring Bean
- 在公司的局域网内,组长让你在自己的虚拟机部署项目,大家等着访问测试,该怎么办???
- 小白成长记——Java基础之操作符
- 星罡:因为期待_你需要更出色!
- Android消息处理机制
- 聊天室入门实战(node,socket.io实现)--第二章(美化,图片发送,登录提示,认证)
- 求最大连续子列和的算法和时间复杂度的分析(包含四种不同时间复杂度的算法)