Fraction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 626 Accepted Submission(s): 352
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/b8a4a111280624f78f82f477edea942b.png)
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
http://acm.split.hdu.edu.cn/showproblem.php?pid=5912
意思就不翻译了 直接模拟就好了
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
if(!b) return a;
else return gcd(b,a%b);
}
int aa[1000],bb[1000];
int a,b,c;
int main()
{
int t,i,j,h,d,n;
scanf("%d",&t);
for(h=1; h<=t; h++)
{
scanf("%d",&n);
for(i=1; i<=n; i++)
scanf("%d",&aa[i]);
for(j=1; j<=n; j++)
scanf("%d",&bb[j]);
aa[0]=0;
a=bb[n];
b=aa[n];
for(i=n; i>=1; i--)
{
if(i!=n)
{
int gg=b;
b=a;
a=bb[i]*gg;
d=gcd(a,b);
a/=d;
b/=d;
}
a=aa[i-1]*b+a;
d=gcd(a,b);
a/=d;
b/=d;
}
printf("Case #%d: %d %d\n",h,a,b);
}
return 0;
}