hdu 5912 Fraction
2018-01-22 15:05
465 查看
hdu 5912 Fraction
标签(空格分隔): ACMMr. 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)); memse 4000 t(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; }
相关文章推荐
- 模拟:Fraction HDU - 5912
- HDU 5912 Fraction (2016-ccpc-长春)
- hdu 5912Fraction
- HDU-5912-Fraction
- HDU 5912 Fraction【模拟】(2016中国大学生程序设计竞赛(长春)B题)
- HDU 5912 - Fraction
- hdu 5912 Fraction
- HDU 5912 Fraction(模拟——分子式化简求解)
- HDU 5912 Fraction
- HDU-5912 Fraction(迭代)
- HDU 5912 Fraction 模拟题 【2016中国大学生程序设计竞赛(长春)】
- hdu 5912 Fractionl 2016ACM/CCPC长春赛区现场赛B
- hdu 5912 Fraction
- hdu 5912 Fraction -2016中国大学生程序设计竞赛(长春)
- HDU 5912 Fraction(模拟——分子式化简求解)
- HDU 5912 Fraction(模拟——分子式化简求解)
- hdu 5912 Fraction 2016中国大学生程序设计竞赛(长春) 水题
- HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))
- hdu 5912 Fraction(除法迭代)
- HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))