本题要求编写程序,计算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. OpenLayers项目分析——(一)项目介绍

    OpenLayers 是由MetaCarta公司开发的,用于WebGIS客户端的JavaScript包,目前的最高版本是2.7 V,通过BSD License 发行.它实现访问地理空间数据的方法都符合 ...

  2. Problem J: 搜索基础之红与黑

    Problem J: 搜索基础之红与黑 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 170  Solved: 100[Submit][Status][ ...

  3. Oracle小技巧_不同表空间不同用户导入导出数据dmp

    [博主注:数据库方面 ITPUB网站及博客内容非常丰富]   转载来源ITPUB 博客 经常有人会问:原来的数据在USERS表空间里面,我想把它IMP进APP表空间,我已经修改了目的用户的默认表空间, ...

  4. 第十二篇、OC_仿淘宝商品详情页的翻页

    // // GFBProductViewController.m // elmsc // // Created by MAC on 2016/11/26. // Copyright © 2016年 G ...

  5. swiper动画效果

    参考swiper官方网站:http://www.swiper.com.cn/ Swiper常用于移动端网站的内容触摸滑动: 结构展示:   纯javascript打造的滑动特效插件,面向手机.平板电脑 ...

  6. 十、Linux vi/vim

    Linux vi/vim 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主 ...

  7. 三十五、MySQL 运算符

    MySQL 运算符 本章节我们主要介绍 MySQL 的运算符及运算符的优先级. MySQL 主要有以下几种运算符: 算术运算符 比较运算符 逻辑运算符 位运算符 算术运算符 MySQL 支持的算术运算 ...

  8. UNIX网络通信

    一.网络协议 国际标准化组织(ISO)定义了网络协议的基本框架,被称为OSI模型.OSI模型包括应用层.表示层.会话层.传输层.网络层.数据链路层及物理层.而OSI模型过于复杂至今没有得到实际的应用. ...

  9. python笔记-dict字典的方法2

    #!/usr/bin/env python #-*- coding:utf-8 -*- ''' 概述: 使用键值(key-value)存储,具有极快的查找速度 注意:字典是无序的 key的特性: 1. ...

  10. 【IDEA】热部署插件Jrebel破解安装

    JRebel 介绍 IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊.目前对于idea热部署最好的解决方案就是安装JRebel插件 ...