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 ...
随机推荐
- C# GDI
绘制实心矩形 using (Graphics gp = Graphics.FromImage(bmBlank)) { //... ; Rectangle rec = , y, , );//画一个白块, ...
- Zoey.Dapper--Dapper扩展之把SQL语句放到文件中
介绍 不知道大家在用Dapper的时候SQL语句是写到哪的,目前看网上的例子都是写到类里面的. 此项目的目的是把SQL语句放到文件(xml)中 目前只是初步版本,只是说明了意图,后面会持续完善和优化 ...
- 【Python发展】pandas和koalas
1.pandas介绍 Python 数据科学在过去几年中爆炸式增长, pandas 已成为生态系统的关键.当数据科学家得到一个数据集时,他们会使用 pandas 进行探索.它是数据处理和分析的终极工具 ...
- AI进阶之路
一.方法论 二.发展趋势 三.入门查看 1. https://hongyuxie.github.io/MyResume_CN/ 上班后大家还刷算法题吗 编程面试的 10 大算法概念汇总 技术面试宝典: ...
- See Elevator Run Floors
“在我短暂的电梯作业中我发现,架构的优化能力是有限的.越是工于优化算法…越是会被自己的架构所制约….想要更好的优化,唯有超越架构………" 零.基础 优化建立在架构之上,这句话莫得问题,也莫得 ...
- P2245 星际导航 瓶颈路
\(\color{#0066ff}{ 题目描述 }\) sideman 做好了回到 \(\text{Gliese}\) 星球的硬件准备,但是 \(\text{sideman}\) 的导航系统还没有完全 ...
- 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm
[bzoj] 1179: [Apio2009]Atm Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri ...
- Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)
题目描述 已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变.比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0] ...
- node mysql问题:Client does not support authentication protocol requested by server; consider upgrading MySQL client!
node后台 mysql处理模块(版本:2.16.0) 执行connect方法时报错: Client does not support authentication protocol requeste ...
- CF796B Find The Bone
Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, numbered from ...