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 ...
随机推荐
- sql优化从300秒到7秒
原始sql select b.jd 街道,b.rglm 楼宇,zzrl 楼宇编号,count(oname) 入楼企业总数, (select count(oname) from ${tablename} ...
- chown virtualbox
virtualbox安装 root@cbill-VirtualBox:/# chown --help用法:chown [选项]... [所有者][:[组]] 文件... 或:chown [选项]... ...
- SQL基础教程(第2版)第4章 数据更新:4-4 事务
●事务是需要在同一个处理单元中执行的一系列更新处理的集合. ● 事务处理的终止指令包括COMMIT(提交处理)和ROLLBACK(取消处理)两种. ● DBMS的事务具有原子性(Atomicity). ...
- VUE,index key v-for
列表渲染语法 v-forv-for 循环对象 <article v-for="(item, key, index) of info">{{item}} {{key}} ...
- awk使用笔记
awk特殊字符打印方法: 1.awk打印双引号: awk '{print "\""}' 2.awk打印单引号: awk '{print "'\''&quo ...
- 吴裕雄--天生自然 JAVA开发学习: 循环结构
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System ...
- IntelliJ IDEA 2019.2 LUA环境搭建说明
1.搭建GCC 添加系统环境变量PATH 为C:\MinGW\bin目录 测试命令进入CMD gcc -v 2.编译LUA cd到lua/src目录 mingw32-make min ...
- numpy(二)
1.集合操作 包含去重,交,并,差集操作 2.排序.搜索和计数 sort,where,argmin,argmax,count_nonzero,argwhere 3.线性代数 np.linalg库,包含 ...
- Bless All
# php code $i = 2333 $myJXOI = JXOI() while($i == 2333){ ++myJXOI.score , ++myJXOI.rp , --myJXOI.常数 ...
- php获取客户IP
获取客户真实IP,保存到数据库建议转整 function getIp(){ $ip = ''; if(!empty($_SERVER['HTTP_CLIENT_IP'])){ $ip = $_SERV ...