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 ...
随机推荐
- Sql 关键字with
我在写一篇时候,被很多同学说没技术含量,实际在开发过程中,我们做递归实际是在数据库端处理,把当前子集所有的都给递归出来.再 程序里再循环匹配的 这样性能就会快多了. 这里涉及到一个sqlserver的 ...
- php 用命令行导出和导入MySQL数据库
命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server 4.1\ ...
- 利用Sharepoint 创建轻量型应用之基本功能配置!
博客同步课程.假设你想跟着视频学习,请跟着例如以下视频: http://edu.csdn.net/course/detail/2097 1. 点击安装程序,出现的界面先期安装完毕准备工具,准备工具 ...
- Extjs学习笔记--(六,选择器)
文档对象dom是javascript与页面元素的桥梁 选择器的作用就是通过元素的标签名,属性名,css属性名对页面进行快速,准确的定位及选择 Extjs的选择器:Ext.DomQuery Ext.qu ...
- Nginx(二)-- 配置文件之虚拟主机配置
1.配置文件与解释 #user nobody; worker_processes 1; # 设置工作子进程,默认是1个工作子进程,可以修改,一般设置为CPU的总核数 #error_log logs/e ...
- 常用的sass编译库
@charset "UTF-8"; /*引进图片合并给一个变量(后面会用到这个变量)*/ $sprites:sprite-map("pwd/*.png",$sp ...
- oneThink的ArticleController控制,详看
本人新手小白,看下 onethink 的 ArticleController , 它里面写的方法,和一些自己以后改进的方向: <?php namespace Home\Controller; c ...
- linux防火墙iptables
2.1 框架图 -->PREROUTING-->[ROUTE]-->FORWARD-->POSTROUTING--> mangle | mangle ^ mangle n ...
- Spring集成Struts、Hibernate----三大框架SSH(Spring、Struts和hibernate)
Spring 框架可以完成业务层的设计任务,Struts框架可以将表示层和业务层分离,而Hibernate框架可以提供灵活的持久层支持.下面介绍三大框架的集成环境: 1.配置Struts2. I.导入 ...
- 6.IIs部署与发布
A.网站的发布步骤: 1.首先要选择要发布的网站(即项目里的网站)也就是代码. 2.左键选择发布. 3.配置文件:Web.congig. 4.连接:publis method:File System, ...