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 ...
随机推荐
- go语言的time.Sleep
首先:time.sleep单位为:1ns (纳秒) 转换单位: 1纳秒 =1000皮秒 1纳秒 =0.001 微秒 1纳秒 =0.000 001毫秒 1纳秒 =0.0 ...
- NHibernate初学二之简单执行SQL及HQL、Linq
上篇文章简单介绍NHibernate之简单增删改查,本文将会简单介绍有关执行NHibernate的SQL.HQL及存储过程: 一:执行SQL语句实例,运用CreateSQLQuery方法 public ...
- mybatis由浅入深day02_2一对一查询_2.2方法一:resultType
2 一对一查询 2.1 需求(查询所有订单信息,关联查询创建订单的用户信息) 查询所有订单信息,关联查询创建订单的用户信息 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用 ...
- Jmeter content-type:multipart/form-data温故
本文讲三种content-type以及在Jmeter中对应的参数输入方式 第一部分:目前工作中涉及到的content-type 有三种: content-type:在Request Headers里, ...
- ios开发之--UIDocumentInteractionController的使用(实现更多分享服务)
最近在做项目的时候,碰到这样一个需求,就是本地生成pdf文件,然后本地打开,经过测试发现,pdf文件是无法保存到相册里面的,只能存到手机里面,鉴于苹果的存储机制,需要取出来,进行本地展示,可以直接传到 ...
- Redis(四)-- 集群
一.Redis适合做企业级分布式缓存集群的条件 1.Redis内置哈希槽,有16384个哈希槽(0~16383),根据CRC16算法来确定这个集群中属于哪一个服务器来处理这个请求. 2.Redis提供 ...
- Python pymysql 模块
pymysql 是 Python3 连接 MySQL 的一个模块,常见用法如下: [root@localhost ~]$ pip3 install pymysql # 安装 pymysql 模块 In ...
- cut的用法【转】
cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇信息分析的. (1)其语法格式为:cut [-bn] [file] 或 cut ...
- 浅谈Socket长连+多线程
缘由 不知各位同仁有没有发现,用简单,无外乎就是都是一个流程 1)监听链接 2)校验链接是否是正常链接 3)保存链接至全局静态字典 4)开启新线程监听监听到的线程报文 5)执行对应命令或者发送对应命令 ...
- AndroidのUI布局之layout weight
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...