Kattis之旅——Rational Arithmetic
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 |
|---|---|
4 |
5 / 6 |
题目不解释了,看样例也能懂。
注意用long long。
//Asimple
#include <bits/stdc++.h>
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==?a:gcd(b, a%b);
} void print(ll num, ll den){
bool pos = (num> && den>) || (num< && den<);
if (num<) num = -num;
if (den<) den = -den;
ll d = gcd(num,den);
num /= d , den /= d;
if (num== || den==) 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,);
break;
case '-':
add_sub(a, b, c, d, -);
break;
case '*':
mul(a, b, c, d);
break;
case '/':
mul(a, b, d, c);
break;
}
}
} int main(){
input();
return ;
}
Kattis之旅——Rational Arithmetic的更多相关文章
- PAT1088:Rational Arithmetic
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 ...
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- PAT_A1088#Rational Arithmetic
Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...
- A1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- Kattis之旅——Prime Reduction
A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...
- 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 ...
随机推荐
- mybatis {arg0} 与 {0}
解决方案: MyBatis的XML的配置文件中声明设置属性的useActualParamName参数值为假 <setting name="useActualParamName" ...
- SQL IN查询优化
实际项目中有如下SQL, 发现效率很低,用时超过1分钟 select TaskID, StartDate = min(UpdateTime), EndDate = max(UpdateTime) fr ...
- shell 中 标准输出和错误输出
命令 标准输出 标准错误 >/dev/null 2>&1 丢弃 丢弃 2>&1 >/dev/null 丢弃 屏幕 1>/dev/null 丢弃 屏幕 2& ...
- [LeetCode] 577. Employee Bonus_Easy tag: SQL
Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...
- 小a和uim之大逃离
题目传送门 #include <bits/stdc++.h> using namespace std; #define ll long long #define re register # ...
- canvas原生js写的贪吃蛇
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 20165321实验一 Java开发环境的熟悉
一.idea调试: 二.实验内容: 实现学生成绩管理功能(增删改,排序,查找),并进行测试(正常情况,异常情况,边界情况). 运行截图: 代码: public class Student { Stri ...
- iOS 开发笔记-控制器tab切换view显示
在开发过程中,我们常常会碰到一种情况就是,在一个controller里面,经常要放很多复杂的控制,最常用的就是tar切换.tar切换,原理就是在一个controller里面,显示另一个controll ...
- Linux的文件最大连接数
[最大连接数]Linux的文件最大连接数 查看当前操作系统连接数设置 ulimit -a ==================================== 修改服务器最大连接数 vim / ...
- jQuery-数据管理-删除事件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...