本题要求编写程序,计算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. [VC]strcpy和strncoy的区别

    第一种情况:char* p="how are you ?";char name[20]="ABCDEFGHIJKLMNOPQRS"; strcpy(name,p ...

  2. Spark性能优化——和shuffle搏斗

    Spark的性能分析和调优很有意思,今天再写一篇.主要话题是shuffle,当然也牵涉一些其他代码上的小把戏. 以前写过一篇文章,比较了几种不同场景的性能优化,包括portal的性能优化,web se ...

  3. handlebars用法

    为什么需要模板引擎 关于前端的模板引擎,我用一个公式来解释 模板引擎 模板 + 数据 ========> html页面 模板引擎就像是html的解析生成器,将对应的模板填充完数据之后生成静态的h ...

  4. SqlServer 学习笔记

    随机函数 select rand() declare @age int set @age = rand()*100 select @age 数据类型转换 declare @birthday datat ...

  5. python 读取mat文件

    import osimport scipy.io as sio import numpy as np #matlab文件名 matfn='/home/user/devkit/data/meta_det ...

  6. 博学谷-数据分析numpy

    import numpy as  np print np.version.version np.array([1,2,3,4]) np.arange(15) np.array(range(10)) = ...

  7. 博学谷-数据分析matplotlib

    博学谷-数据分析 python数学学科的基础 机器学习课程的基础 1.1 介绍 1.2 jupyter和conda 1.3 matplotlib from matplotlib import pypl ...

  8. VueX源码分析(5)

    VueX源码分析(5) 最终也是最重要的store.js,该文件主要涉及的内容如下: Store类 genericSubscribe函数 resetStore函数 resetStoreVM函数 ins ...

  9. 牛客小白月赛5 A 无关(relationship) 【容斥原理】【数据范围处理】

    题目链接:https://www.nowcoder.com/acm/contest/135/A 题目描述 若一个集合A内所有的元素都不是正整数N的因数,则称N与集合A无关.   给出一个含有k个元素的 ...

  10. ELFhash - 优秀的字符串哈希算法

    ELFhash - 优秀的字符串哈希算法 2016年10月29日 22:12:37 阅读数:6440更多 个人分类: 算法杂论算法精讲数据结构 所属专栏: 算法与数据结构   版权声明:本文为博主原创 ...