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
1 3 + 1 2
1 3 - 1 2
123 287 / 81 -82
12 -3 * -1 -1
5 / 6
-1 / 6
-82 / 189
-4 / 1

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

注意用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==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;
}
时间: 2024-11-06 05:44:05

Kattis之旅——Rational Arithmetic的相关文章

1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise

题目信息 1088. Rational Arithmetic (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input fi

pat1088. Rational Arithmetic (20)

1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Spe

SICP ex2-1 modified rational arithmetic

经过十多天煎熬,本宝宝终于顺利进入第二章,傲娇脸 首先,本章章首简要回顾了上一章的基本内容以及总体概述了data abstraction 的原因重要性,以及介绍了本章将会学习的一些东西 2.1.1通过一个rational arithmetic的例子来引出data abstraction 并且介绍基础pair 语法 ex2-1要求修改书中所给的make-rat使之能够在给定参数为-时,将符号放在分子出输出,两种思路 一 修改gcd 使之能够处理负数 二 利用abs 将gcd 参数约束为正数,并保留

PAT1088. Rational Arithmetic

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

1088. Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

Kattis之旅——Prime Reduction

A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composite integer is one which is not prime. The fundamental theorem of arithmetic says that any integer x can be expressed uniquely as a set of prime factors

A1088. Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

A.1088 Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

PAT (Advanced Level) 1088. Rational Arithmetic (20)

简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algor