[PAT] 1081 Rational Sum (20 分)Java

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 package pattest;
 2
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6
 7 /**
 8  * @Auther: Xingzheng Wang
 9  * @Date: 2019/2/26 21:26
10  * @Description: pattest
11  * @Version: 1.0
12  */
13 public class PAT1081 {
14     public static void main(String[] args) throws IOException {
15         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
16         String n = reader.readLine();
17         String[] s1 = reader.readLine().split(" ");
18         String[] s2 = s1[0].split("/");
19         int a = Integer.parseInt(s2[0]), b = Integer.parseInt(s2[1]), sum = 0;
20         for (int i = 1; i < Integer.parseInt(n); i++) {
21             String[] s = s1[i].split("/");
22             int atemp = Integer.parseInt(s[0]);
23             int btemp = Integer.parseInt(s[1]);
24             a = a * btemp + atemp * b;
25             b = b * btemp;
26             int c = GCD(a, b);
27             a = a / c;
28             b = b / c;
29         }
30         if (a % b == 0) {
31             System.out.print(a / b);
32         } else {
33             if (Math.abs(a) > Math.abs(b)) {
34                 if (a > 0) {
35                     System.out.printf("%d %d/%d", a / b, a - a / b * b, b);
36                 } else {
37                     a = -a;
38                     System.out.printf("-%d %d/%d", a / b, a - a / b * b, b);
39                 }
40             } else {
41                 System.out.printf("%d/%d", a - a / b * b, b);
42             }
43         }
44     }
45
46     private static int GCD(int m, int n) {
47         return n == 0 ? m : GCD(n, m % n);
48     }
49 }

原文地址:https://www.cnblogs.com/PureJava/p/10498125.html

时间: 2024-10-12 07:56:27

[PAT] 1081 Rational Sum (20 分)Java的相关文章

【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

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

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

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

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

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.约分为:&

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

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 <