1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为“300”,一开始卡在里这里,

测试用例:

24/8 100/10

24/11 300/11

2.该题用到了欧几里德算法求最小公约数gcd(a,b)

算法如下:

//欧几里德算法求最大公约数gcd,其中a>b
long long gcd(long long a, long long b)
{
return b == 0 ? a : gcd(b, a%b);
}

AC代码如下:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std; /*
24/8 100/10
24/11 300/11
*/ //欧几里德算法求最大公约数gcd
long long gcd(long long a, long long b)
{
return b == 0 ? a : gcd(b, a%b);
}
void str2num(string str, long long&top, long long&bot, int&sign)
{
bool first = true;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == '-')
sign = -1;
else if (str[i] != '/'&&first)
top = top * 10 + str[i] - '0';
else if (str[i] == '/')
first = false;
else if (str[i] != '/'&&!first)
bot = bot * 10 + str[i] - '0';
}
}
string i2s(long long a)
{
string ans = "";
if (a == 0) return "0";
else
{
while (a != 0)
{
char c = a % 10 + '0';
ans = c + ans;
a /= 10;
}
}
return ans;
}
string int2Str(long long top, long long bot, bool sign)
{
long long tmpGCD = gcd(top, bot);
top /= tmpGCD;
bot /= tmpGCD;
long long tmpInt = top / bot;
long long tmpRat = top%bot;
string ans = "";
if (tmpInt != 0)
{
ans = i2s(tmpInt);
}
if (tmpRat == 0 && ans.size() != 0)
;
else if (tmpRat == 0 && ans.size() == 0)
{
return "0";
}
else if (tmpRat != 0 && ans.size() != 0)
{//整数和分数同时存在
ans += " "+i2s(tmpRat) + "/" + i2s(bot);
}
else if (tmpRat != 0 && ans.size() == 0)
{//仅存在分数
ans += i2s(tmpRat) + "/" + i2s(bot);
}
if (!sign)
ans = "(-" + ans + ")";
return ans;
}
int main(void)
{
string a, b;
cin >> a >> b;
long long aTop = 0, aBot = 0, bTop = 0, bBot = 0;
int aSign = 1;
int bSign = 1;
str2num(a, aTop, aBot, aSign);
str2num(b, bTop, bBot, bSign);
string aAns;
string bAns;
if (aTop != 0)
{
int aGCD = gcd(aTop, aBot);
aTop /= aGCD;
aBot /= aGCD;
aAns = int2Str(aTop, aBot, aSign == 1);
}
else aAns = "0";
if (aTop != 0)
{
int bGCD = gcd(bTop, bBot);
bTop /= bGCD;
bBot /= bGCD;
bAns = int2Str(bTop, bBot, bSign == 1);
}
else
bAns = "0"; //加法:
long long addBot = aBot*bBot;
long long addTop = aSign*aTop*bBot + bSign*bTop*aBot;
bool addSign = (addTop >= 0 ? true : false);
addTop = labs(addTop);
long long addInt = addTop / addBot;
string addAns = int2Str(addTop, addBot, addSign);
cout << aAns << " + " << bAns << " = " << addAns << endl; //减法:
long long diffBot = aBot*bBot;
long long diffTop = aSign*aTop*bBot - bSign*bTop*aBot;
bool diffSign = (diffTop >= 0 ? true : false);
diffTop = labs(diffTop);
long long diffInt = diffTop / diffBot;
string diffAns = int2Str(diffTop, diffBot, diffSign);
cout << aAns << " - " << bAns << " = " << diffAns << endl; //乘法
long long proBot = aBot*bBot;
long long proTop = aSign*bSign*aTop*bTop;
bool proSign = (proTop >= 0 ? true : false);
proTop = labs(proTop);
long long proInt = proTop / proBot;
string proAns = int2Str(proTop, proBot, proSign);
cout << aAns << " * " << bAns << " = " << proAns << endl; //除法
long long quoBot = aBot*bTop;
long long quoTop = aSign*bSign*aTop*bBot;
string quoAns;
if (quoBot != 0)
{
bool quoSign = (quoTop >= 0 ? true : false);
quoTop = labs(quoTop);
long long quoInt = quoTop / quoBot;
quoAns = int2Str(quoTop, quoBot, quoSign);
}
else
quoAns = "Inf";
cout << aAns << " / " << bAns << " = " << quoAns << endl; return 0;
}

1088. Rational Arithmetic (20)的更多相关文章

  1. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

  2. PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

    输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...

  3. PAT (Advanced Level) 1088. Rational Arithmetic (20)

    简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...

  4. 【PAT甲级】1088 Rational Arithmetic (20 分)

    题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...

  5. pat1088. Rational Arithmetic (20)

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...

  6. PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]

    1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...

  7. 1088 Rational Arithmetic(20 分)

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...

  8. PAT Rational Arithmetic (20)

    题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...

  9. PAT 1088. Rational Arithmetic

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...

随机推荐

  1. 8. react 基础 - props 默认值和类型限制 与 Props , State , render 函数 关系

    一. PropTypes 与 DefaultProps 官方文档 1. PropTypes 属性校验 引入 PropTypes import PropTypes from 'prop-types'; ...

  2. Adobe PhotoShop CS6中文破解版下载

    在网上找了好多个PhotoShop破解版,但安装过程中都出现一些问题,用不了.现在找到一个比较小的PhotoShop CS6安装包,大小200M左右,下载解压,点击安装就可以使用了,安装过程十分简单. ...

  3. rewrite例子集合

    在 httpd 中将一个域名转发到另一个域名 虚拟主机世界近期更换了域名,新域名为 www.wbhw.com, 更加简短好记.这时需要将原来的域名 webhosting-world.com, 以及论坛 ...

  4. HTML5 Canvas——基础入门

    认识canvas html5的新标签 <canvas>标签只是图像容器,必须使用js来绘制图形 可以通过多种方法使用canvas绘制路径,盒,圆,字符以及添加图像 canvas画布 < ...

  5. VUE- 访问服务器端数据 Vue-resource

    VUE- 访问服务器端数据 Vue-resource 1. 安装 vue-resource cnpm install vue-resource --save 安装完毕后,在main.js中导入,如下所 ...

  6. 实验吧web-易-what a fuck!这是什么鬼东西?

    打开链接是一大串符号,是js编码的一种,全部复制下来,粘贴在控制台中回车就拿到flag了.

  7. PHP的一个小tips (关于=和==或者===的使用)

    由于我在项目中,很多场景判断等式成立的时候 都习惯把值放在==前面(例如 1 == $nStatus), 今天有个同事揪着我问为啥总这样写,回答之后今天也稍作记录下吧. 如果正常些 $nStatus ...

  8. PAT-输入输出

    测试样例输入方式 while...EOF型(题目没有给定输入的结束条件) while(~scanf("%s",s)) {} //等价于while(scanf("%s&qu ...

  9. Ubuntu apt install 下载软件很慢的解决办法

    1.打开/etc/apt/sources.list 将内容替换为以下内容(注意把sources.list文件备份一下) deb http://mirrors.aliyun.com/ubuntu/ xe ...

  10. 程序Dog的大梦想

    一.我是程序狗 "怎么又是任宏啊,每天都起这么早,要命啊--" "人家任宏可是要成为学霸的男人,咱们这些凡夫俗子啊,理解不了这么伟大的理想--"----微电影& ...