本题要求编写程序,计算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的更多相关文章

  1. PAT1034;Head of a Gang

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  2. PAT1034. Head of a Gang ——离散化+并查集

    题意:成员A与成员B通话 ,成员B与成员C通话,则 ABC即为一个团伙,一共有若干个团伙,每个团伙的人数大于2且相互通话时间超过一定值即为黑帮,每个黑帮伙里有一个BOSS,boss是与各个成员打电话最 ...

  3. pat1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

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

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

随机推荐

  1. World Wind Java开发之九——阶段小结(转)

    http://blog.csdn.net/giser_whu/article/details/42785875 将近一个月没有更新了,一是因为项目的事情,二是期末考试复习,三是玩啦.上一篇博客搭建起了 ...

  2. POJ 3126 Prime Path(筛法,双向搜索)

    题意:一个4位的素数每次变动一个数位,中间过程也要上素数,问变成另一个的最小步数. 线性筛一遍以后bfs就好.我写的双向,其实没有必要. #include<cstdio> #include ...

  3. flex在众多手机浏览器上的兼容方案(亲测华为手机自带浏览器)

    如果项目使用构建工具,可加autoprefixer来处理,[autoprefixer使用指南] 纯手写css兼容代码,需给每个使用的属性加上属性前缀 /*display: flex;写法*/ span ...

  4. 阿里云服务器下安装LAMP环境(CentOS Linux 6.3) 安装与配置 FTP 服务器

    我们经常会使用 FTP,把本地电脑上的文件上传到服务器上,或者把服务器上的文件下载到自己的电脑里面.FTP 有服务端和客户端,FTP 的服务端提供了这种传输文件的服务,FTP 的客户端提供了传输文件的 ...

  5. 问题005:如何配置JDK,Java运行环境?

    方法一:我的电脑右击-->属性-->高级-->环境变量-->Path 方法二:set path是查询环境变灵, set path=路径

  6. C/C++程序基础 (五)位运算

    C++中四种转换运算符的区分 const_cast 修改const和volatile属性 reinterpret_cast 指针间类型转换或者指针和整形的转换.二进制重新翻译. static_cast ...

  7. JavaScript 循环

    for循环:  如果您希望一遍又一遍运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: document.write(cars[0] + "<br ...

  8. 第37课 thinkphp5添加商品基本信息及通过前置钩子上传商品主图 模型事件(勾子函数)

    目录 手册地址: before_insert(新增之前的操作) 要实现的功能 思路 触发条件: 1. 控制器里必须要调用模型的save()方式保存数据,用insert()触发不了勾子函数的 2. 模型 ...

  9. Django runserver支持https

    创建自签名ssl证书 1.下载软件openssl-0.9.8k_WIN32 2.解压后进入bin目录,双击打开openssl.exe,依次运行如下命令 genrsa -des3 -out server ...

  10. django处理上传文件配置

    1.sttings中配置 'django.template.context_processors.media' TEMPLATES = [ { 'BACKEND': 'django.template. ...