PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
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
题目大意:给出两个分数,并给出其加减乘除的结果表示,在除法时,如果分子为0,那么输出Inf。
//虽然从来没做过这样的题目,但是感觉还是比较简单的。
#include <stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int main() {
long int a[],b[];
long int re1[],re2[],re3[],re[];
scanf("%ld/%ld %ld/%ld",a[],a[],b[],b[]);
//那么这个就是求最小公倍数,最大公因数的问题了。
long int ming=max(a[],b[]);
long long x=a[]*b[];
for(int i=ming;i<=x;i++){
if(i%a[]==&&i%b[]==){
ming=i;break;
}
}
a[]=a[]/a[];
b[]=b[]/b[];
a[]=a[]*ming/a[];
b[]=b[]*ming/b[];
a[]=ming;
b[]=ming;
re1[]
//需要存好多种形式的结果啊! return ;
}
//自己写着写着就写不下去了。没有底。各种细节,感觉控制不了。
代码来自:https://www.liuchuo.net/archives/1906
#include <iostream>
#include<stdio.h>
using namespace std;
long long int a, b, c, d;//使用这种方法需要数据的范围很大。 long long int gcd(long long int t1, long long int t2) {
return t2 == ? t1 : gcd(t2, t1 % t2);
} void func(long long int m, long long int n) {
int flag1 = ;
int flag2 = ;
if (n == ) {//分母肯定是不能为0得,如果有1/3 0/1这样的输入,在加法中,分母会变成3而不是0.
cout << "Inf";
return ;
}
if (m == ) {
cout << ;
return ;
} if (m < ) {
m = - m;
flag1 = ;
}
if (n < ) {
n = - n;
flag2 = ;
}
int flag = ;
if (flag1 == && flag2 == ) {
flag = ;
} else if (flag1 == || flag2 == ) {
flag = ;//这个主要是来确定整个结果包括分子和分母的符号
}
if (m == n) {
if (flag == )
cout << "(-1)";//计算结果为负值,需要加括号
else
cout << "";//计算结果为正值,不需要加。
return;
} long long int x = m % n;//因为m不可能=0了,之前已经判断过了,所以此处
//如果x为0,那么肯定就是没有余数。
long long int y = m / n;
if (x == ) {
if (flag == )
cout << y;
else
cout << "(-" << y << ")";
return ;
} else {
long long int t1 = m - y * n;
long long int t2 = n;
long long int t = gcd(t1, t2);
t1 = t1 / t;
t2 = t2 / t;
if (flag == ) {
cout << "(-";
if (y != )//假分数
cout << y << " " << t1 << "/" << t2;
else
cout << t1 << "/" << t2;
cout << ")";
} else {//真分数
if (y != )
cout << y << " " << t1 << "/" << t2;
else
cout << t1 << "/" << t2;
}
}
} void add() {
long long int m, n;//还没见过这个数据类型
m = a * d + b * c;//直接这样得出分子,厉害。
n = b * d;//分母。
func(a, b);//处理方式都是一样的。
cout << " + ";
func(c, d);
cout << " = ";
func(m, n);
cout << endl;
} void min() {
long long int m, n;
m = a * d - b * c;
n = b * d;
func(a, b);
cout << " - ";
func(c, d);
cout << " = ";
func(m, n);
cout << endl;
} void multi() {
long long int m, n;
m = a * c;
n = b * d;
func(a, b);
cout << " * ";
func(c, d);
cout << " = ";
func(m, n);
cout << endl;
} void div() {
long long int m, n;
m = a * d;
n = b * c;
func(a, b);
cout << " / ";
func(c, d);
cout << " = ";
func(m, n);
cout << endl;
} int main() {
scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
add();
min();
multi();
div();
return ;
}
//学习了!各种细节考虑的都很好。
1.对每一个数都有一个函数进行处理,十分简洁。
2.由于数可能会非常大,所以使用了long long int这种数据类型
3.顺便复习了,如何求两个数得最大公因数。!
PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]的更多相关文章
- PAT 1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- 1088 Rational Arithmetic
题意: 给出两个分式(a1/b1 a2/b2),分子.分母的范围为int型,且确保分母不为0.计算两个分数的加减乘除,结果化为最简的形式,即"k a/b",其中若除数为0的话,输出 ...
- PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算
输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- 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 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- 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 ...
随机推荐
- 0060 Spring MVC的数据类型转换--ConversionService--局部PropertyEditor--全局WebBindingInitializer
浏览器向服务器提交的数据,多是字符串形式,而有些时候,浏览器需要Date.Integer等类型的数据,这时候就需要数据类型的转换器 使用Spring的ConversionService及转换器接口 下 ...
- cocos2d 中使用jni C++ 调用 Java 方法
1.简单数据类型样例 如果我们Java中有这么一个open的静态方法,它没有參数,有一个int的返回值.怎么在C++中调用它呢? package cb.CbCCBLE; public class Cb ...
- python2.0_s12_day19_前端模版使用
Django中引用bootstrap实现在前端可以创建客户信息,可以修改客户信息我们需要设计一个前端用户交互系统.我们在设计之前,讨论一些需求:前端实现:1. 不同角色的用户,看到的东西是不一样的 销 ...
- Android ImageResizer:inSampleSize
import android.annotation.TargetApi; import android.content.Context; import android.content.res.Reso ...
- SPOJ - DQUERY
题目链接:传送门 题目大意:一个容量 n 的数组, m次询问,每次询问 [x,y]内不同数的个数 题目思路:主席树(注意不是权值线段树而是位置线段树) 也就是按一般线段树的逻辑来写只是用主席树实现而已 ...
- ipmi监控主机
author: headsen chen date: 2018-09-25 20:04:01 IPMI介绍 IPMI(Intelligent Platform Management ...
- centos6.5安装sendmail
1.下载安装sendEmail(下载绿色版,解压可直接使用) wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1. ...
- 关于Visual Studio 20**自动添加头部注释信息
作为一个万年潜水党,不管这一篇文章技术含量如何,也算是一个好的开始吧. 在日常的开发中我们经常需要为类库添加注释和版权等信息,这样我们就需要每次去拷贝粘贴同样的文字,为了减少这种重复性的工作,我们 ...
- 微信小程序 --- 缓存数据
保存数据 / 读取数据 / 删除数据 / 数据异步操作 每一个微信小程序都可以有自己的本地缓存,可以通过wx.setStorage( wx.setStorageSync) ,wx.getS ...
- 收集一些常用的CDN链接!无需下载快速使用!
一些常用的CDN链接,可以到这里看: http://www.bootcdn.cn/ 这个网站查找资源的方式很简单,后缀加上要查找的名字即可: 例如: http://www.bootcdn.cn/boo ...