1088. Rational Arithmetic (20)
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)的更多相关文章
- PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]
题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...
- PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算
输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验 ...
- PAT (Advanced Level) 1088. Rational Arithmetic (20)
简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath&g ...
- 【PAT甲级】1088 Rational Arithmetic (20 分)
题意: 输入两个分数(分子分母各为一个整数中间用'/'分隔),输出它们的四则运算表达式.小数需要用"("和")"括起来,分母为0的话输出"Inf&qu ...
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT 1088 Rational Arithmetic[模拟分数的加减乘除][难]
1088 Rational Arithmetic(20 分) For two rational numbers, your task is to implement the basic arithme ...
- 1088 Rational Arithmetic(20 分)
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- PAT Rational Arithmetic (20)
题目描写叙述 For two rational numbers, your task is to implement the basic arithmetics, that is, to calcul ...
- PAT 1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
随机推荐
- C++交换两个变量值的方法
简单地列一下交换两个变量值地几种方法. 1.通过第三方实现,这一种也是最最最常见普通的方法: void swap(int *a, int *b) { int tmp = *a; *a = *b; *b ...
- vue form 验证
vue 验证 <Form :model="formModel" label-position="center" :label-width="90 ...
- dp--P1439 最长公共子序列(LCS)
题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入格式 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式 一个数,即最长公共子序列的长度 找出两 ...
- LeetCode——155. 最小栈
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...
- (转载)Tomcat 报错 (The tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config is mi)
错误如图所示: 目前对于这个错误的原因尚不清楚,目前只知道如何解决这个错误,等到以后知道了原因之后再更改此文. 原因猜测: 之前你的eclipse关联的tomcat由于某种原因出现了信息丢失,需要重新 ...
- 5.GIT使用问题
1.git命令显示总是像less 一样的效果问题 git config --global pager.branch false
- python format输出
http://www.cnblogs.com/nulige/p/6115793.html 2.Format 方式 [[fill]align][sign][#][0][width][,][.precis ...
- usr/sbin/inetd
root 4 0.0 1344 1204? S 17:09 0:10 /usr/sbin/inetd 运行 Internet 超级 服务器,它负责监听 Internet sockets 上的连接,并调 ...
- 推荐Markdown编辑器——Inspire
推荐Markdown编辑器--Inspire Inspire是一款非常好用的编辑器,支持Markdown语法,当然,Inspire还有一些自己的语法. 本文就是在这款编辑器下编写的. 风格 像Visu ...
- 干货 | 玩转云文件存储——利用CFS实现web应用的共享访问
京东云文件服务(Cloud File Service,以下简称:CFS)是一种高可靠.可扩展.可共享访问的全托管分布式文件系统.它可在不中断应用服务的情况下,根据您对文件系统的使用,按需扩展或缩减,并 ...