Fraction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1274 Accepted Submission(s): 659
Problem Description
Mr. Frog recently studied how to add two fractions up, and he came up with an evil idea to trouble you by asking you to calculate the result of the formula below:
As a talent, can you figure out the answer correctly?
![](https://i-blog.csdnimg.cn/blog_migrate/86cffb6384e1e5484e689c911886c2de.jpeg)
As a talent, can you figure out the answer correctly?
Input
The first line contains only one integer T, which indicates the number of test cases.
For each test case, the first line contains only one integer n ( n≤8
).
The second line contains n integers: a
1
,a
2
,⋯a
n
(1≤a
i
≤10
).
The third line contains n integers: b
1
,b
2
,⋯,b
n
(1≤b
i
≤10)
.
For each test case, the first line contains only one integer n ( n≤8
The second line contains n integers: a
The third line contains n integers: b
Output
For each case, print a line “Case #x: p q”, where x is the case number (starting from 1) and p/q indicates the answer.
You should promise that p/q is irreducible.
You should promise that p/q is irreducible.
Sample Input
1 2 1 1 2 3
Sample Output
Case #1: 1 2HintHere are the details for the first sample: 2/(1+3/1) = 1/2
Source
//2016中国大学生程序设计竞赛(长春)-重现赛
//#include"stdafx.h"
#include<iostream>
using namespace std;
#define size 10
int a[size],b[size];
int n;
struct score
{
int fenzi;
int fenmu;
};
score calculate() {
score sum;
sum.fenzi = a[n];
sum.fenmu = 1;
for (int i = n; i >= 2; i--) {
int temp = sum.fenmu;
sum.fenmu = sum.fenzi;
sum.fenzi = a[i - 1] * sum.fenzi + b[i] * temp;
}
return sum;
}
int getgcd(int a, int b) {
if (b == 0)
return a;
else
return getgcd(b, a%b);
}
int main() {
int test;
cin >> test;
for (int cases = 1; cases <= test; cases++) {
cin >> n;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
score temp = calculate();
int p = b[1]*temp.fenmu;
int q = temp.fenzi;
int gcd = getgcd(p, q);
cout << "Case #" << cases << ": " << p / gcd << " " << q / gcd << endl;
}
}