pat1088. Rational Arithmetic (20)
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
分情况讨论。其实代码可以写的更加简洁的,很多过程是重复的。
#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)的更多相关文章
- PAT1088:Rational Arithmetic
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算
输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...
- PAT (Advanced Level) 1088. Rational Arithmetic (20)
简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...
- 【PAT甲级】1088 Rational Arithmetic (20 分)
题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...
- 1088. Rational Arithmetic (20)
1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
随机推荐
- vue2.x学习笔记
1.使用模板template的时候必须要有跟节点,可以支持表达式,但不支持正则,想使用正则就用过滤器. 2.数据在显示的时候所带的HTML DOM直接显示,不会渲染,要渲染DOM,得用v-html. ...
- STM32单片机串口中断+DMA使用(含CUBE配置)
最近又要重新用32做点东西,发现一两年没怎么碰的结果就是,曾经熟得不行的东西都变得极度陌生,这种重新学习记忆的过程过于痛苦,果然还是要留下一些记录给之后失忆的自己的. 1.STM32CUBE配置 1. ...
- 【转】新建网站(CodeFile)与新建Web应用(Codebehind)的区别
源地址:http://www.cnblogs.com/harry0906/articles/3575725.html
- v-touch使用方法以及在项目中遇到的问题
上篇博客中我记得还有一个坑没有解决好,在这篇博客中详细说明一下. 在 https://github.com/dreamITGirl/vuepageturn 我的这个代码库里,更新到2.1版本. 目前解 ...
- php屏蔽错误消息
定义和用法: error_reporting() 设置 PHP 的报错级别并返回当前级别. 函数语法: error_reporting(report_level) 如果参数 level 未指定,当前报 ...
- Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) B 1075B (思维)
B. Taxi drivers and Lyft time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 2017年6月15日 由一个freemarker出错引发的感想
今天想要实现一个功能,想要实现遍历多个checkbox的功能.想出一个解决方法用了30秒钟,将包含的键值put进map中,前台根据map[key]??判断是否具有该值,乍一看这个方法很好,可是实际上问 ...
- Codeforces Round #335 (Div. 2) B
B. Testing Robots time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces - 722C 区间合并
要求断裂的数列之和的最大值,只需在断裂处的下标修改为一个足够负无穷大的值就可以用线段树维护 这道题数据还是弱了点,如果n和ai均取最大可能我这个程序早就爆ll了(4e4的时候炸了),毕竟本来想着用GC ...
- 安装openstack时遇到的错误
学习opensatck的第一步是安装DevStack来进行本机操作 1. 下面命令没有权限,解决办法:切换到root用户下执行sudo -s echo "stack ALL=(ALL) NO ...