1088. Rational Arithmetic (20)
来源:互联网 发布:58速运 知乎 编辑:程序博客网 时间:2024/06/05 16:39
给定分子分母能准确给出其标准形式,期间需要求最大公约数,注意负数还有加括号的处理
#include <iostream>#include <cstdio>#include <sstream>using namespace std;long long gcd(long long a, long long b){while(b){long long r = a%b;a = b;b = r;}return a;}string format(long long n, long long d){if(n == 0) return "0";string s;stringstream ss;bool neg = n < 0;if(neg){n = -n;ss << "(-";}long long r = gcd(n, d);n /= r;d /= r;if(n%d == 0){ss << n;}else if(n > d){ss << n/d;ss << " ";ss << n%d << "/" << d;}else{ss << n << "/" << d;}if(neg){ss << ")";}getline(ss, s);return s;}int main(){long long a1,b1,a2,b2;scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);// +long long sum1 = a1*b2 + a2*b1;long long sum2 = b1*b2;// -long long sub1 = a1*b2 - a2*b1;long long sub2 = b1*b2;// *long long mul1 = a1*a2;long long mul2 = b1*b2;printf("%s + %s = %s\n", format(a1, b1).c_str(), format(a2, b2).c_str(), format(sum1, sum2).c_str());printf("%s - %s = %s\n", format(a1, b1).c_str(), format(a2, b2).c_str(), format(sub1, sub2).c_str());printf("%s * %s = %s\n", format(a1, b1).c_str(), format(a2, b2).c_str(), format(mul1, mul2).c_str());// /if(a2 == 0){printf("%s / %s = Inf\n", format(a1, b1).c_str(), format(a2, b2).c_str());}else{long long quo1 = a1*b2;long long quo2 = b1*a2;if(a2 < 0){quo1 = -quo1;quo2 = -quo2;}printf("%s / %s = %s\n", format(a1, b1).c_str(), format(a2, b2).c_str(), format(quo1, quo2).c_str());}return 0;}
0 0
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- 1088. Rational Arithmetic (20)
- bananapi之DSI
- 将一个数表示成任意两个数的平方和
- Android内存优化四:从代码角度进行优化
- unix 各种返回信号详解
- 完美解决eclipse时创建新项目时出错问题
- 1088. Rational Arithmetic (20)
- Android内存优化五:Android垃圾回收器机制
- 二层交换机、三层交换机和路由器的原理及区别
- block 定义
- STM32F103 定时器+ADC+DMA中断+双缓存+实现数据采集
- Python图像处理库PIL的ImageOps模块介绍
- Windows环境下编译lua
- 时间获取以及其他相关
- zookeeper集群搭建