博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Kattis之旅——Rational Arithmetic
阅读量:4686 次
发布时间:2019-06-09

本文共 1914 字,大约阅读时间需要 6 分钟。

Input

The first line of input contains one integer, giving the number of operations to perform.

Then follow the operations, one per line, each of the form x1 y1 op x2 y2. Here, −109≤x1,y1,x2,y2<109 are integers, indicating that the operands are x1/y1 and x2/y2. The operator op is one of ’+’, ’−’, ’∗’, ’/’, indicating which operation to perform.
You may assume that y1≠0, y2≠0, and that x2≠0 for division operations.

Output

For each operation in each test case, output the result of performing the indicated operation, in shortest terms, in the form indicated.

 

Sample Input 1 Sample Output 1
41 3 + 1 21 3 - 1 2123 287 / 81 -8212 -3 * -1 -1
5 / 6-1 / 6-82 / 189-4 / 1

题目不解释了,看样例也能懂。

注意用long long。

//Asimple#include 
using namespace std;typedef long long ll;ll n, m, s, res, ans, len, T, k, num;ll a, b, c, d;char ch;ll gcd(ll a, ll b ) { return b==0?a:gcd(b, a%b);}void print(ll num, ll den){ bool pos = (num>0 && den>0) || (num<0 && den<0); if (num<0) num = -num; if (den<0) den = -den; ll d = gcd(num,den); num /= d , den /= d; if (num==0 || den==0) cout << "0 / 1" << endl; else cout << (pos?"":"-") << num << " / " << den << endl;}void add_sub(ll x1, ll y1, ll x2, ll y2, int state){ ll num = x1*y2 + state*x2*y1; ll den = y1*y2; print(num,den);}void mul(ll x1, ll y1, ll x2, ll y2){ ll num = x1*x2; ll den = y1*y2; print(num,den);}void input() { cin >> T; while( T -- ) { cin >> a >> b >> ch >> c >> d; switch(ch){ case '+': add_sub(a, b, c, d,1); break; case '-': add_sub(a, b, c, d, -1); break; case '*': mul(a, b, c, d); break; case '/': mul(a, b, d, c); break; } }}int main(){ input(); return 0;}

 

转载于:https://www.cnblogs.com/Asimple/p/6773648.html

你可能感兴趣的文章
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>
黑马程序员 ExecuteReader执行查询
查看>>
记一些从数学和程序设计中体会到的思想
查看>>
题目1462:两船载物问题
查看>>
POJ 2378 Tree Cutting(树形DP,水)
查看>>
第二冲刺阶段个人博客5
查看>>
UVA 116 Unidirectional TSP (白书dp)
查看>>
第三方测速工具
查看>>
MySQL 网络访问连接
查看>>
在aws ec2上使用root用户登录
查看>>
数据访问 投票习题
查看>>
CIO知识储备
查看>>
cnblog!i'm coming!
查看>>
使用点符号代替溢出的文本
查看>>
Axios 中文说明
查看>>