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?
As a talent, can you figure out the answer correctly?
For each test case, the first line contains only one integer n ( n≤8 ).
The second line contains n integers: a1,a2,⋯an(1≤ai≤10 ).
The third line contains n integers: b1,b2,⋯,bn(1≤bi≤10)
You should promise that p/q is irreducible.
1 2 1 1 2 3
Case #1: 1 2
Here are the details for the first sample: 2/(1+3/1) = 1/2注意数据范围的要求即可import java.util.*; public class Main { static Scanner in = new Scanner(System.in); static int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } public static void main(String[] args) { int cnt = in.nextInt(); int[] a=new int[10]; int[] b=new int[10]; int cas=0; while(cnt-->0){ cas++; int n=in.nextInt(); for (int i = 1; i <= n; i++) a[i]=in.nextInt(); for (int i = 1; i <= n; i++) b[i]=in.nextInt(); if(n==1){ int p=b[1]/gcd(b[1],a[1]); int q=a[1]/gcd(b[1],a[1]); System.out.println("Case #"+cas+": "+ p+" "+q); } else{ int sum1=a[n]*b[n-1]; int sum2=a[n]*a[n-1]+b[n]; int k=gcd(sum1,sum2); sum1/=k; sum2/=k; int p=sum1; int q=sum2; for(int i=n-2;i>=1;i--){ sum1=b[i]*q; sum2=a[i]*q+p; k=gcd(sum1,sum2); p=sum1/k; q=sum2/k; } System.out.println("Case #"+cas+": "+ p+" "+q); } } } }