hdu 5912 Fraction

版权声明:本文为博主原创文章,转载请附上链接出处。 https://blog.csdn.net/allen_li123/article/details/79129204

hdu 5912 Fraction

标签(空格分隔): ACM


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?

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≤8n≤8).
The second line contains n integers: a1,a2,⋯an(1≤ai≤10a1,a2,⋯an(1≤ai≤10).
The third line contains n integers: b1,b2,⋯,bn(1≤bi≤10)b1,b2,⋯,bn(1≤bi≤10).

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.

Sample Input

1
2
1 1
2 3

Sample Output

Case #1: 1 2

Hint

Here are the details for the first sample:
2/(1+3/1) = 1/2

AC代码

#include<iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
int  T,n;
int a[15],b[15];
struct frac{
    int a=0,b=0;
};
int gcd(int a,int b)
{
    int m;
    if( a < b)
        m = a,a=b,b=m;
    while(a%b){
        m = a%b;
        a = b;
        b = m;
    }
    return b;
}
frac getAns(int i,int n)
{
    frac temp;
    if(i==n){
        temp.a=b[i];
        temp.b=a[i];
    }else {
        frac x=getAns(i+1,n);
        temp.a=b[i]*x.b;
        temp.b=a[i]*x.b+x.a;
    }
    return temp;
}

int main()
{
    cin>>T;
    frac answer;
    for(int k=1;k<=T;k++)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        cin>>n;
        for(int i=1;i<=n;i++)    cin>>a[i];
        for(int i=1;i<=n;i++)    cin>>b[i];
        answer=getAns(1,n);
        int g=gcd(answer.a,answer.b);
        answer.a/=g;    answer.b/=g;
        printf("Case #%d: %d %d\n",k,answer.a,answer.b);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/allen_li123/article/details/79129204
今日推荐