本题要求编写程序,计算 2 个有理数的和、差、积、商。

输入格式:

输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为 0。

输出格式:

分别在 4 行中按照 有理数1 运算符 有理数2 = 结果 的格式顺序输出 2 个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式 k a/b,其中 k 是整数部分,a/b 是最简分数部分;若为负数,则须加括号;若除法分母为 0,则输出 Inf。题目保证正确的输出中没有超过整型范围的整数。

输入样例 1:

2/3 -4/2

输出样例 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

输入样例 2:

5/3 0/6

输出样例 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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 200 ms
内存限制: 64 MB
代码长度限制: 16 KB
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
const int maxn = ;
struct num{
long long k = ;
long long up;
long long down;
};
int gcd(long long a, long long b){
return b == ? a : gcd(b, a % b);
}
num add(num n1, num n2){
num res;
res.down = n1.down*n2.down;
res.up = n1.down*n2.up + n1.up*n2.down;
return res;
}
num sub(num n1, num n2){
num res;
res.down = n1.down*n2.down;
res.up = n2.down*n1.up - n2.up*n1.down;
return res;
}
num mul(num n1, num n2){
num res;
res.down = n1.down*n2.down;
res.up = n1.up*n2.up;
return res;
}
num dive(num n1, num n2){
num res;
res.down = n1.down*n2.up;
res.up = n1.up*n2.down;
if (res.down < ){
res.down = - * res.down;
res.up = - * res.up;
}
return res;
}
void clean(num n){
int flag = ;
n.k = n.up / n.down;
if (n.up < ){
n.up = -n.up;
flag = ;
}
n.up = n.up%n.down;
int g = abs(gcd(n.up, n.down));
n.up /= g;
n.down /= g;
if (flag == ){
printf("(");
}
if (n.k != ){
printf("%lld", n.k);
if (n.up != ){
printf(" %lld/%lld", n.up, n.down);
}
}
else{
if (n.up != ){
if (flag == )printf("-");
printf("%lld/%lld", n.up, n.down);
}
else{
printf("");
}
}
if (flag == ){
printf(")");
} }
int main(){
num n1, n2;
scanf("%lld/%lld %lld/%lld", &n1.up, &n1.down, &n2.up, &n2.down);
/*int g = abs(gcd(n1.up, n1.down));
n1.up /= g;
n1.down /= g;
g = abs(gcd(n2.up, n2.down));
n2.up /= g;
n2.down /= g;*/
num q, w, e, r;
q = add(n1, n2);
clean(n1);
printf(" + ");
clean(n2);
printf(" = ");
clean(q);
printf("\n"); w = sub(n1, n2);
clean(n1);
printf(" - ");
clean(n2);
printf(" = ");
clean(w);
printf("\n"); r = mul(n1, n2);
clean(n1);
printf(" * ");
clean(n2);
printf(" = ");
clean(r);
printf("\n"); if (n2.up != ){
e = dive(n1, n2);
clean(n1);
printf(" / ");
clean(n2);
printf(" = ");
clean(e);
printf("\n");
}
else{
clean(n1);
printf(" / ");
clean(n2);
printf(" = Inf\n"); } system("pause");
}

注意点:这道题的坑有两个,一个是int不够大,两个大int乘起来就爆了,要用long long,long long 的输入输出要用lld。第二个是约分,求最大公约数,遍历就超时了,要用辗转相除法,一定要记住!!!

PAT B1034 有理数四则运算 (20 分)的更多相关文章

  1. PAT1034 有理数四则运算 (20分)

    1034 有理数四则运算 (20分)   本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全 ...

  2. PAT 1034 有理数四则运算(20)(代码框架+思路+测试点错误分析)

    1034 有理数四则运算(20)(20 分)提问 本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的 ...

  3. 1034 有理数四则运算 (20 分)C语言

    题目描述 本题要求编写程序,计算2个有理数的和.差.积.商. 输入描述: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整 ...

  4. PAT 1034. 有理数四则运算(20)

    本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只 ...

  5. PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)

    PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)  http://www.patest.cn/contests/pat-b-practise/1034 ...

  6. PAT-乙级-1034. 有理数四则运算(20)

    1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求编写程序,计算2个有理 ...

  7. PAT Basic 1034 有理数四则运算(20) [数学问题-分数的四则运算]

    题目 本题要求编写程序,计算2个有理数的和.差.积.商. 输⼊格式: 输⼊在⼀⾏中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分⼦和分⺟全是整型范围内的整数, ...

  8. 【算法笔记】B1034 有理数四则运算

    1034 有理数四则运算 (20 分)   本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母 ...

  9. pat 1035 Password(20 分)

    1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the ...

随机推荐

  1. Java - List总结

    Java提高篇(三二)-----List总结 前面LZ已经充分介绍了有关于List接口的大部分知识,如ArrayList.LinkedList.Vector.Stack,通过这几个知识点可以对List ...

  2. HDU1029(KB12-B)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  3. DRF序列化

    1. 安装 pip install djangoframework 2. app注册 rest_framework INSTALLED_APPS = [ 'django.contrib.admin', ...

  4. TFS 打得你措手不及!TF53001:管理员已取消数据库操作

    心塞.公司TFS突然挂了.签入获取 一直报 TF53001:管理员已取消数据库操作.公司开发部开发进度一下就受阻了.刚好有时关键时期. 在 老总的帮助下根据搜到的资料 .搞定了这个问题!问题出在数据库 ...

  5. 安装nvm管理不同的node版本

    在工作或者学习中,偶尔会遇到需要切换不同node版本的需求,幸好有神器nvm可以帮我们解决问题.下面我们就来讲解如何在window系统上安装nvm!

  6. 工作记录(JS向textarea添加固定内容、通过固定字符将字符串分割为数组)

    第一个是在 textarea 输入框中添加固定的内容. 代码如下: <textarea id="text" cols="30" rows="10 ...

  7. angularjs1.X获取前一天日期

    $scope.getDate = new Date(); //可以当前日期 $scope.nowDate = $filter("date")($scope.getDate, &qu ...

  8. python + Jenkins + requests 数据驱动接口测试 环境部署

    ** Jenkins安装: * 安装包选择:Jenkins.war * windows下有msi和war两种格式,我使用的是war,下载下来丢到xmapp的指定目录就行,操作方便一点      * m ...

  9. 使用angular-cli脚手架快速搭建项目

    第一步 安装全局的angular-cli, npm install -g @angular/cli 或者 cnpm install -g @angular/cli@v1.0.0-rc.2 – 国内淘宝 ...

  10. 脚本设置IP bat 命令行设置自动获取IP和固定IP

    由于办公室网络需要固定IP和DNS才能上网, 在连接公共网络或者家里又需要自动获取IP和DNS才能上网. 频繁手动切换很麻烦,就搞了两个脚本一键设置. 1.新建文本文件, 命名为固定IP.bat 复制 ...