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 ...
随机推荐
- 学习Shell(一)
查看 Shell Shell 是一个程序,一般都是放在/bin或者/user/bin目录下,当前 Linux 系统可用的 Shell 都记录在/etc/shells文件中./etc/shells是一个 ...
- Learn golang: Top 30 Go Tutorials for Programmers Of All Levels
https://stackify.com/learn-go-tutorials/ What is Go Programming Language? Go, developed by Google in ...
- drf解析器
1.简介 作用:将传过来的数据,解析成字典 2.使用 分为局部使用和全局使用 局部使用,什么都不写,默认就是 parser_classes = [JSONParser,FormParser] from ...
- sap 程序之间的相互调用
1:首先进入到local object 目录下. 右键>create >function group,创建一个函数组. 右键创建类其它的东西 2:在创建的function group(fu ...
- Spark Mllib之分层抽样
Spark中组件Mllib的学习之基础概念篇 1.解释 分层抽样的概念就不讲了,具体的操作: RDD有个操作可以直接进行抽样:sampleByKey和sample等,这里主要介绍这两个 (1)将字符串 ...
- 《Java程序设计》第一周学习记录(1)
目录 Windows安装JDK.Git Linux下安装JDK.Git.IDEA 参考资料 Windows安装JDK.Git 到官网直接下载JDK,双击安装程序就正常安装就行了. 下载完以后,可以看到 ...
- 一个简单的sel server 函数的自定义
创建自定义函数:use 数据库名gocreate function 函数名(@pno int)returns intasbegin declare @a int if not exists(se ...
- mysql----------局域网数据库:如何让navicat链接局域网其他的数据库。
1.找到被链接的数据库,打开以后有一个自带的mysql数据库,打开以后下面有一个user表,把里面的第一条数据的第一个字段改成% 百分号,然后保存,重启数据库,搞定 2.如果是linux下的话,记得把 ...
- LeetCode88.合并两个有序数组
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m 和 n. ...
- LeetCode69.x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...