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 ...
随机推荐
- VMWare虚拟机提示:打不开磁盘…或它所依赖的某个快照磁盘,开启模块DiskEarly的操作失败,未能启动虚拟机
将电脑上存在的虚拟机复制一份后打开运行,弹出错误提示: 打不开磁盘…或它所依赖的某个快照磁盘,开启模块DiskEarly的操作失败,未能启动虚拟机. 解决方法如下: 打开存放虚拟机系统硬盘的所在文件夹 ...
- docker search 详解
docker search 用于从 Docker Hub(https://hub.docker.com)中搜索指定的镜像,用法如下: [root@localhost ~]$ docker search ...
- 华为P10闪存门
随着余承东的倡议书以及五一假期3天的时间冲刷,华为的闪存门事件,似乎被冲淡了.但相信还有很多人对华为“闪存门”的起始及发展过程不是特别了解.而华为作为2017年Q1季度手机出货量的冠军,居然在4月份出 ...
- iOS开发 - 检测网络状态(WIFI、2G/3G/4G)
本文转载至 http://blog.csdn.net/wangzi11322/article/details/45580917 检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是 ...
- 上传图片Security Error
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjIAAACXCAIAAACA4CZ6AAAgAElEQVR4nOy96Xcd13UnugFSUrJer/ ...
- springboot---->springboot的使用(一)
这里我们记录一下springboot的使用,第一次创建环境. springboot的使用 项目结构如下: 一.我们使用maven去构建springboot的依赖.其中我们使用的pom.xml文件内容如 ...
- JS-cookie封装
智能社学习笔记 <script type="text/javascript"> /*****设置cookie*****/ function setCookie(name ...
- jQuery数据缓存
jQuery引入数据缓存机制的原因: 1.储存更DOM节点相关的数据.事件.动画等信息 2.用一种低耦合的方式让DOM节点和数据联系起来 实现原理: 1.jQuery内部创建cache对象 2.为需要 ...
- Ubuntu远程登录服务器--ssh的安装和配置
ssh是一种安全协议,主要用于给远程登录会话数据进行加密,保证数据传输的安全. 安装ssh sudo apt-get update sudo apt-get install openssh-serve ...
- 【BZOJ4554】[Tjoi2016&Heoi2016]游戏 二分图最大匹配
[BZOJ4554][Tjoi2016&Heoi2016]游戏 Description 在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂.简单的说,这个游戏就是在一张地图上放上若干个炸弹,看 ...