本题要求编写程序,计算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. 【BZOJ4866】[YNOI2017] 由乃的商场之旅(莫队)

    点此看题面 大致题意: 给你一个字符串,每次给你一段区间,问这段区间内有多少个字符串在重新排列后可以变成一个回文串. 关于莫队 详见这篇博客:莫队算法学习笔记(一)--普通莫队. 关于回文 要使一个字 ...

  2. 123apps-免费网络应用

    前言 在Jianrry`s博客看见推荐这个网址,试用了一下感觉还不错.主要是完全免费!!就当备用吧 网站介绍 123apps 网站地址:https://123apps.com/cn/ 旗下网站: PD ...

  3. 2018.6.27 Ajax实现异步刷新

    Servlet获取URL地址.在HttpServletRequest类里,有以下六个取URL的函数: getContextPath 取得项目名 getServletPath 取得Servlet名 ge ...

  4. 2018.2.8 php实现qq登陆接口

    PHP实现QQ登录的原理和实现过程 2018-02-08 学习与分享 PHP自学中心 第三方登录,就是使用大家比较熟悉的比如QQ.微信.微博等第三方软件登录自己的网站,这可以免去注册账号.快速留住用户 ...

  5. javaweb基础(26)_jsp标签库开发二

    一.JspFragment类介绍 javax.servlet.jsp.tagext.JspFragment类是在JSP2.0中定义的,它的实例对象代表JSP页面中的一段符合JSP语法规范的JSP片段, ...

  6. 短短几行css代码实现滚动条效果

    如何实现使用css实现滚动条效果 实现效果,运用线性渐变来实现功能 假设我们的页面被包裹在 <body> 中,可以滚动的是整个 body,给它添加这样一个从左下到到右上角的线性渐变: bo ...

  7. yum仓库及配置

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于服务器需求,需要在公司内网搭建内网yum源. 搭建内网yum源需要分以下几个步骤,如下: 1. yum是什么 2. repo文件是什么 3. r ...

  8. Spring中注解大全和应用

    @Controller@RestController:@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable@C ...

  9. Python9-IO模型-day41

    # 进程:启动多个进程,进程之间是由操作系统负责调用# 线程:启动多个线程,真正由被cpu执行的最小单位实际是线程# 开启一个线程,创建一个线程,寄存器.堆栈# 关闭一个线程# 协程# 本质上是一个线 ...

  10. 2015-2016 Northwestern European Regional Contest (NWERC 2015)

    训练时间:2019-04-05 一场读错三个题,队友恨不得手刃了我这个坑B. A I J 简单,不写了. C - Cleaning Pipes (Gym - 101485C) 对于有公共点的管道建边, ...