1081. Rational Sum (20)【模拟】——PAT (Advanced Level) Practise

题目信息

1081. Rational Sum (20)

时间限制400 ms

内存限制65536 kB

代码长度限制16000 B

Given N rational numbers in the form “numerator/denominator”, you are supposed to calculate their sum.

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (<=100), followed in the next line N rational numbers “a1/b1 a2/b2 …” where all the numerators and denominators are in the range of “long int”. If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:

For each test case, output the sum in the simplest form “integer numerator/denominator” where “integer” is the integer part of the sum, “numerator” < “denominator”, and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1:

5

2/5 4/15 1/30 -2/60 8/3

Sample Output 1:

3 1/3

Sample Input 2:

2

4/3 2/3

Sample Output 2:

2

Sample Input 3:

3

1/3 -1/6 1/8

Sample Output 3:

7/24

解题思路

模拟加法,注意结果中分母负号和分子为0情况

AC代码

#include <cstdio>
struct node{
    long long a, b;
    node(long long a, long long b):a(a), b(b){}
};
long long gcd(long long a, long long b){
    return b ? gcd(b, a%b) : a;
}
node add(node& a, node& b){
    node r(a.a*b.b + a.b*b.a, a.b*b.b);
    long long t = gcd(r.a, r.b);
    r.a /= t;
    r.b /= t;
    return r;
}
int main()
{
    int n;
    long long a, b;
    scanf("%d", &n);
    scanf("%lld/%lld", &a, &b);
    node p(a, b);
    for (int i = 1; i < n; ++i){
        scanf("%lld/%lld", &a, &b);
        node t(a, b);
        p = add(p, t);
    }
    if (p.b < 0){
        p.b *= -1;
        p.a *= -1;
    }
    if (p.a >= p.b){
        printf("%lld", p.a/p.b);
        if (p.a % p.b){
            printf(" %lld/%lld", p.a%p.b, p.b);
        }
    }else{
        if (p.a == 0){
            printf("0");
        }else{
            printf("%lld/%lld", p.a, p.b);
        }
    }
    printf("\n");
    return 0;
}
时间: 2024-10-11 15:29:02

1081. Rational Sum (20)【模拟】——PAT (Advanced Level) Practise的相关文章

PAT甲题题解-1081. Rational Sum (20)-模拟分数计算

模拟计算一些分数的和,结果以带分数的形式输出注意一些细节即可 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; /* 模拟计算一些分数的和,结果以带分数的形式输出 注意一些细节即可 */ const int maxn=105; const int maxv=50000

A.1081 Rational Sum (20)

1081 Rational Sum (20)(20 分) Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum. Input Specification: Each input file contains one test case. Each case starts with a positive integer N (<=100

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

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

PAT (Advanced Level) 1081. Rational Sum (20)

简单模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; struct FenShu { long l

1104. Sum of Number Segments (20)【数学题】——PAT (Advanced Level) Practise

题目信息 1104. Sum of Number Segments (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence {0.1, 0.2, 0.3, 0.4}, we have 10 segments: (0.1)

1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise

题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new n

【PAT甲级】1081 Rational Sum (20 分)

题意: 输入一个正整数N(<=100),接着输入N个由两个整数和一个/组成的分数.输出N个分数的和. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[107],b[107];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; for(int i

PAT:1081. Rational Sum (20) AC(类似math.h中的sqrt(1.0*n),algorithm的abs()用于取绝对值)

#include<stdio.h> #include<algorithm> using namespace std; typedef long long ll; //[skill]重命名 struct num { ll zi,mu; //分子分母 }; ll gcd(ll a,ll b) //求最大公约数 { return b==0 ? a:gcd(b,a%b); } num yuefen(num a) //分数约分 { //printf("%lld/%lld.约分为:&