PAT1034
本题要求编写程序,计算2个有理数的和、差、积、商。
输入格式:
# include<iostream>
# include<algorithm>
# include<stdio.h>
# include<string>
using namespace std;
bool ISint(int a,int b)
{
double c = a*1.0 / b;
if(c - (int)c == )
{
return true;
}
else
{
return false;
}
}
int gcd(int a,int b)//a da
{
int t;
if(a<b)
{
t=a;a=b;b=t;
} if(b==)
{
return ;
}
else
{
while(b!=)
{
t = a % b;
a = b;
b = t;
}
return a;
}
} int gbs(int x,int y)
{
return (x*y)/gcd(x,y);
} void printhe(int he,int gb,int a1,int b1,int a2,int b2)
{
if( ISint(a1,b1) && a1>=)
{
cout<<a1/b1;
}
else if(ISint(a1,b1) && a1< )
{
cout<<"("<<a1/b1<<")";
}
else if(!ISint(a1,b1) && a1>=)
{
int t1 = gcd(a1,b1);
a1 = a1 /t1;
b1 = b1 /t1;
if(a1>b1)
{
int k1=;
while(a1>b1)
{
a1 = a1 - b1;
k1++;
}
cout<<k1<<" "<<a1<<"/"<<b1;
}
else
{
cout<<a1<<"/"<<b1;
}
}
else
{
a1 = -a1;
int t1 = gcd(a1,b1);
a1 = a1 /t1;
b1 = b1 /t1;
if(a1>b1)
{
int k1=;
while(a1>b1)
{
a1 = a1 - b1;
k1++;
}
cout<<"(-"<<k1<<" "<<a1<<"/"<<b1<<")";
}
else
{
cout<<"(-"<<a1<<"/"<<b1<<")";
} }
cout<<" + ";
if( ISint(a2,b2) && a2>=)
{
cout<<a2/b2;
}
else if(ISint(a2,b2) && a2< )
{
cout<<"("<<a2/b2<<")";
}
else if(!ISint(a2,b2) && a2>=)
{
int t2 = gcd(a2,b2);
a2 = a2 /t2;
b2 = b2 /t2;
if(a2>b2)
{
int k2=;
while(a2>b2)
{
a2 = a2 - b2;
k2++;
}
cout<<k2<<" "<<a2<<"/"<<b2;
}
else
{
cout<<a2<<"/"<<b2;
}
}
else
{
a2 = -a2;
int t2 = gcd(a2,b2);
a2 = a2 /t2;
b2 = b2 /t2;
if(a2>b2)
{
int k2=;
while(a2>b2)
{
a2 = a2 - b2;
k2++;
}
cout<<"("<<-k2<<" "<<a2<<"/"<<b2<<")";
}
else
{
cout<<"(-"<<a2<<"/"<<b2<<")";
} }
cout<<" = ";
if(he>=)
{
if(ISint(he,gb))
{
cout<<he;
}
else
{
int t = gcd(he,gb);
he = he /t;
gb = gb /t;
if(he>gb)
{
int k=;
while(he>gb)
{
he = he - gb;
k++;
}
cout<<k<<" "<<he<<"/"<<gb;
}
else
{
cout<<he<<"/"<<gb;
}
}
}
else
{
he = - he;
if(ISint(he,gb))
{
cout<<"(-"<<he/gb<<")";
}
else
{
int t = gcd(he,gb);
he = he /t;
gb = gb /t;
if(he>gb)
{
int k=;
while(he>gb)
{
he = he - gb;
k++;
}
cout<<"("<<-k<<" "<<he<<"/"<<gb<<")";
}
else
{
cout<<"("<<-he<<"/"<<gb<<")";
}
}
}
cout<<endl;
} void printcha(int cha,int gb,int a1,int b1,int a2,int b2)
{
if( ISint(a1,b1) && a1>=)
{
cout<<a1/b1;
}
else if(ISint(a1,b1) && a1< )
{
cout<<"("<<a1/b1<<")";
}
else if(!ISint(a1,b1) && a1>=)
{
int t1 = gcd(a1,b1);
a1 = a1 /t1;
b1 = b1 /t1;
if(a1>b1)
{
int k1=;
while(a1>b1)
{
a1 = a1 - b1;
k1++;
}
cout<<k1<<" "<<a1<<"/"<<b1;
}
else
{
cout<<a1<<"/"<<b1;
}
}
else
{
a1 = -a1;
int t1 = gcd(a1,b1);
a1 = a1 /t1;
b1 = b1 /t1;
if(a1>b1)
{
int k1=;
while(a1>b1)
{
a1 = a1 - b1;
k1++;
}
cout<<"("<<-k1<<" "<<a1<<"/"<<b1<<")";
}
else
{
cout<<"(-"<<a1<<"/"<<b1<<")";
}
}
cout<<" - ";
if( ISint(a2,b2) && a2>=)
{
cout<<a2/b2;
}
else if(ISint(a2,b2) && a2< )
{
cout<<"("<<a2/b2<<")";
}
else if(!ISint(a2,b2) && a2>=)
{
int t2 = gcd(a2,b2);
a2 = a2 /t2;
b2 = b2 /t2;
if(a2>b2)
{
int k2=;
while(a2>b2)
{
a2 = a2 - b2;
k2++;
}
cout<<k2<<" "<<a2<<"/"<<b2;
}
else
{
cout<<a2<<"/"<<b2;
}
}
else
{
a2 = -a2;
int t2 = gcd(a2,b2);
a2 = a2 /t2;
b2 = b2 /t2;
if(a2>b2)
{
int k2=;
while(a2>b2)
{
a2 = a2 - b2;
k2++;
}
cout<<"("<<-k2<<" "<<a2<<"/"<<b2<<")";
}
else
{
cout<<"(-"<<a2<<"/"<<b2<<")";
}
}
cout<<" = ";
if(cha>=)
{
if(ISint(cha,gb))
{
cout<<cha;
}
else
{
int t = gcd(cha,gb);
cha = cha /t;
gb = gb /t;
if(cha>gb)
{
int k=;
while(cha>gb)
{
cha = cha - gb;
k++;
}
cout<<k<<" "<<cha<<"/"<<gb;
}
else
{
cout<<cha<<"/"<<gb;
}
}
}
else
{
cha = - cha;
if(ISint(cha,gb))
{
cout<<"(-"<<cha/gb<<")";
}
else
{
int t = gcd(cha,gb);
cha = cha /t;
gb = gb /t;
if(cha>gb)
{
int k=;
while(cha>gb)
{
cha = cha - gb;
k++;
}
cout<<"("<<-k<<" "<<cha<<"/"<<gb<<")";
}
else
{
cout<<"("<<-cha<<"/"<<gb<<")";
}
}
}
cout<<endl;
}
int main()
{
int a1,a2,b1,b2;
scanf("%d/%d %d/%d",&a1,&b1,&a2,&b2);
int gb,gy,he,cha;
gb = gbs(b1,b2);
he = a1 * gb / b1 + a2 * gb / b2;
cha = a1 * gb / b1 - a2 * gb / b2;
printhe(he,gb,a1,b1,a2,b2);
printcha(cha,gb,a1,b1,a2,b2);
return ;
}
输入在一行中按照“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
PAT1034的更多相关文章
- PAT1034;Head of a Gang
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- PAT1034. Head of a Gang ——离散化+并查集
题意:成员A与成员B通话 ,成员B与成员C通话,则 ABC即为一个团伙,一共有若干个团伙,每个团伙的人数大于2且相互通话时间超过一定值即为黑帮,每个黑帮伙里有一个BOSS,boss是与各个成员打电话最 ...
- pat1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- PAT1034 有理数四则运算 (20分)
1034 有理数四则运算 (20分) 本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式: 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全 ...
随机推荐
- 【BZOJ2754】[SCOI2012] 喵星球上的点名(后缀数组+莫队)
点此看题面 大致题意: 每个人的名字由姓和名构成,如果某次点名点到的字符串是某人姓或名的一个子串,则这个人就被点到了.求每次点名被点到的人的个数及每个人被点到的总次数. 后缀数组+莫队 这道题做法很多 ...
- 【转】Web实时通信之Socket.IO ,真正的兼容ie
前面两篇文章使用了Ajax long polling和WebSocket两种常用的Web实时通信方式构建了简单的聊天程序. 但是,由于浏览器的兼容问题,不是所有的环境都可以使用WebSocket这种比 ...
- 课堂使用的Linux命令
1.ls 显示目录文件 2.ls -l = ll 显示 3.vi 创建于编辑 4.mv 改名 5. ./ 当前目录 6.chmod 修改权限 4 读 2 编辑 1 运行 7.去除UI界面 syste ...
- el-upload控件一次接口请求上传多个文件
el-upload组件默认情况下上传多少个文件就会请求多少次上传接口,如何一次上传多个文件而不必多次请求上传接口呢?直接看代码 html <el-upload :action="act ...
- Hotkeys.js 2.0.2 发布,JS 网页快捷键设置,捕获键盘输入和输入的组合键快捷键,它没有依赖
这是一个强健的 Javascript 库用于捕获键盘输入和输入的组合键,它没有依赖,压缩只有只有(~3kb),gzip:1.9k. 更新内容: 添加测试用例: 添加更多特殊键支持: 修复bug. __ ...
- 牛客NOIP提高组R1 A中位数(二分)
题意 题目链接 Sol 很神仙的题目啊,考场上只会$n^2$的暴力.. 考虑直接二分一个$mid$,我们来判断最终答案是否可能大于$x$. 判断的时候记录一下前缀最小值即可, 设$s[i]$表示$1- ...
- C/C++程序基础 (八)数据结构
非递归先序遍历 // 输出, 遍历左子树,遍历右子树 void firstOrder(Node* root) { stack<Node*> leftNodes; Node* curr = ...
- docker-compose 使用
Docker提供一个容器编排工具------>Docker Compose,它允许用户在一个模板(YAML格式)中定义一组相关联的应用容器,这组容器会根据配置模板中的"--link&q ...
- 【PHP】php中json_decode()和json_encode()
1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...
- ZendFramework-2.4 源代码 - 关于MVC - View层 - 视图渲染器、视图插件管理器
<?php // 1. 视图渲染器 class PhpRenderer implements Renderer, TreeRendererInterface { /** * 插件管理器 */ p ...