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. 前后端分离java、jwt项目进行CORS跨域、解决非简单请求跨域问题、兼容性问题

    情况描述: 最近在部署一个前后端分离的项目出现了跨域问题*, 项目使用jwt进行鉴权,需要前端请求发起携带TOKEN的请求*,请求所带的token无法成功发送给后端, 使用跨域后出现了兼容性问题:Ch ...

  2. OSS 图片处理流程

    1.步骤一 2.步骤二 3.步骤三 4.步骤四 5.步骤五(步骤4完成会自动添加cname用户解析,不需要自己去加,只需要点击进来看下是否添加成功即可) 通过以上步骤就可以实现了图片服务的配置

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

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

  4. PAT-输入输出

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

  5. shell中sparksql语句调试、执行方式

    1.命令方式执行sparksql查询 SQL="use mydatatable;;select count(1) from tab_videousr_onlne where p_regiio ...

  6. 探讨 Git 代码托管平台的若干问题 - 2019 版

    关于 Git 版本控制软件种类繁多,维基百科收录的最早的版本控制系统是 1972 年贝尔实验室开发的 Source Code Control System.1986 年 Concurrent Vers ...

  7. Spring Cloud Hystrix熔断器隔离方案

      Hystrix组件提供了两种隔离的解决方案:线程池隔离和信号量隔离.两种隔离方式都是限制对共享资源的并发访问量,线程在就绪状态.运行状态.阻塞状态.终止状态间转变时需要由操作系统调度,占用很大的性 ...

  8. 转发和重定向简介及与之相关的(URL)参数(parameter)、属性(attribute)问题探讨

    1.引子 转发和重定向是我们在做web项目中常用到的两个术语,有必要理清两者的区别和与之相关的参数.属性获取问题. 2.转发和重定向 1).转发 转发是服务器行为,将当前请求(Request)和响应( ...

  9. 有几个水洼(DFS)

    #include <iostream> #include<cstdio> using namespace std; #define maxn 105 char field[ma ...

  10. JavaSE--RMI初识

    转自:http://blog.csdn.net/guyuealian/article/details/51992182 一.Java RMI机制:        远程方法调用RMI(Remote Me ...