本题要求编写程序,计算 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生成代码(字节码)

    一.方式 代码生成器 & IDE 编译时代码生成: Pluggable Annotation Processing API 运行时代码生成: Compiler API 运行时生成字节码: cg ...

  2. CSS隐藏多余的文字

    效果: <p><strong>商品名称:</strong>新鲜现摘云南绥江半边红李子甜脆脱骨李6斤当季绿色有机水果包邮</p></div> ...

  3. npm 安装指定模块版本

    npm list  查看具体模块 如: npm list @antv/g6 如需要安装指定的模块和版本 保存时      - --save-dev 是你开发时候依赖的东西,--save 是你发布之后还 ...

  4. ionic 上拉菜单(ActionSheet)安装和iOS样式不一样

    ISO中的界面是这样的: 然而,Android中的界面是这样的: 代码如下: HTML部分: <body ng-app="starter" ng-controller=&qu ...

  5. linux 查找匹配文件中包含指定字符的 前五行,这里是指所有匹配的前五行

    最近被问到 一个关于查找匹配字符的信息显示问题: 系统/etc/sysctl.conf文件会定义系统内核的一些配置,请查找和net有关的信息,并只打印前面5行信息. 解决方式大概试两种写法均可: 1. ...

  6. UWP开发细节记录:IStream 和 IRandomAccessStream^ 以及 IMFByteStream 互转

    IStream 和 IRandomAccessStream^ 互转 IRandomAccessStream^ --> IStream:  CreateStreamOverRandomAccess ...

  7. CSS 小结笔记之解决flex布局边框对不齐

    在使用flex 进行伸缩布局的时候,经常会给子盒子设置边框,这时经常会出现上下边框对不齐的情况.本篇文章来探讨并解决这个问题. 具体出现的问题如下图所示 具体代码如下 <!DOCTYPE htm ...

  8. .NET、ADO.NET、ASP.NET名称解析及.NET平台架构组成

    转https://blog.csdn.net/xiaouncle/article/details/53265256 名词解释 1.Winform:Windows应用程序.桌面应用程序.C/S应用程序  ...

  9. SQL2005中的事务与锁定(九)-(1)- 转载

    ------------------------------------------------------------------------ -- Author : HappyFlyStone - ...

  10. How HashMap works in java 2

    https://www.javacodegeeks.com/2014/03/how-hashmap-works-in-java.html   Most common interview questio ...