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 ...
随机推荐
- vant - 弹框 【Popup 弹出层】【DatetimePicker 时间选择】
[HelloWorld.vue] <template> <div class="hello"> <van-row class="m-head ...
- Pro*C介绍
内嵌SQL 概要 Pro*C语法 SQL 预处理指令 语句标号 宿主变量 基础 指针 结构 数组 指示器变量 数据类型同等化 动态SQL 事务 错误处理 SQLCA WHENEVER语句 Demo程序 ...
- 【Java】-NO.16.EBook.4.Java.1.002-【疯狂Java讲义第3版 李刚】- 数据类型
1.0.0 Summary Tittle:[Java]-NO.16.EBook.4.Java.1.002-[疯狂Java讲义第3版 李刚]- 数据类型 Style:EBook Series:Java ...
- jenkins借助winscp传本地文件到远程服务器上
有这样的场景,我们的ftp上都是些重要的资料,所以大家基本只有可看的权限,只有部分管理人员有可读可写的权限,但是jenkins上基本使用的都是ftp的路径,这个时候就存在一些问题,某些开发需要将自己构 ...
- 正则表达式匹配域名、网址、url
DNS规定,域名中的标号都由英文字母和数字组成,每一个标号不超过63个字符,也不区分大小写字母.标号中除连字符(-)外不能使用其他的标点符号.级别最低的域名写在最左边,而级别最高的域名写在最右边.由多 ...
- Python_summary
Q: python中出现IndentationError:unindent does not match any outer indentation levelA:复制代码的时候容易出现缩进错误,虽然 ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- MyBatis基础入门《三》Select查询集合
MyBatis基础入门<三>Select查询集合 描述: 代码新增了一个MybatisUtil工具类,查询数据库返回集合的时候,接收数据的三种方式.由于代码会渐渐增多,未涉及改动过的文件不 ...
- .NET 黑魔法 - 自定义日志扩展
我们开发程序时避免不了要有日志系统,我们希望有一个通用的.不夹杂任何方言的调用方式,简单地说就是保留微软日志框架的注入方式和使用方式. 比如我们希望这样调用: 我们不希望有个 IAbcLogger, ...
- Python全栈-day8-day9-函数1
函数 day8 1.为什么需要函数 1)代码的组织结构不清晰,可读性差 2)需要重复使用某个功能时,需要重新编写成程序,重复率高 3)多处引用相同代码时,需要扩展功能的时候过于麻烦,工作量大 2.函数 ...