A1081. Rational Sum (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), 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
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 #include <algorithm>
 8 using namespace std;
 9 //求最大公约数
10 int gcd(long long  a,long long b)
11 {
12     if(b==0)return a;
13     else return gcd(b,a%b);
14 }
15
16 struct Fraction{
17     long long up;
18     long long down;
19 };
20 Fraction  reduction(Fraction r)
21 {
22     if(r.down<0)
23     {
24         r.up*=(-1);
25         r.down*=(-1);
26     }
27     if(r.up==0)
28     {
29         r.down=1;
30     }else
31     {
32         int d=gcd(r.up,r.down);
33         r.up=r.up/d;
34         r.down/=d;
35     }
36     return r;
37 }
38
39 Fraction add(Fraction a,Fraction b)
40 {
41     Fraction r;
42     r.down=a.down*b.down;
43     r.up=a.up*b.down+a.down*b.up;
44     return reduction(r);
45 }
46
47 void show(Fraction a)
48 {
49     if(a.down==1)
50     printf("%lld",a.up);
51     else if(abs(a.up)>a.down)
52     {
53         printf("%lld %lld/%lld",a.up/a.down,abs(a.up)%a.down,a.down);
54     }
55     else
56     {
57         printf("%lld/%lld",a.up,a.down);
58     }
59 }
60 int main(){
61     int n;
62     scanf("%d",&n);
63     Fraction sum,temp;
64     sum.up=0;sum.down=1;
65     for(int i=0;i<n;i++)
66     {
67         scanf("%lld/%lld",&temp.up,&temp.down);
68         sum=add(sum,temp);
69     }
70     show(sum);
71     return 0;
72 }
时间: 2024-10-25 14:51:39

A1081. Rational Sum (20)的相关文章

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 star

pat1081. Rational Sum (20)

1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum. Input Specification: Each input file contains one

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

1081. Rational Sum (20)

时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 sta

1081. Rational Sum (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), followed in the ne

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

newcoder PAT 1001 Rational Sum (20)

#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> using namespace std; int gcd(int a,int b) { return b == 0 ? a: gcd(b,a%b); } int s[105]; int x[105]; int main() { int n; cin >> n; for(int i = 0;i <

【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 (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