题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5912
1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 int gcd(int a,int b) 5 { 6 return b==0? a : gcd(b,a%b); 7 } 8 int main() 9 { 10 int T,t=1,n,i,a[11],b[11],k; 11 scanf("%d",&T); 12 while(T--) 13 { 14 scanf("%d",&n); 15 for(i=1;i<=n;i++) scanf("%d",&a[i]); 16 for(i=1;i<=n;i++) scanf("%d",&b[i]); 17 int fz=b[n],fm=a[n]; 18 for(i=n-1;i>=1;i--) 19 { 20 fz += fm*a[i]; 21 fm *= b[i]; 22 swap(fz,fm); 23 } 24 k=gcd(fz,fm); 25 printf("Case #%d: %d %d\n",t++,fz/k,fm/k); 26 } 27 return 0; 28 }
HDU 5912 Fraction(模拟——分子式化简求解)
原文:http://www.cnblogs.com/wenzhixin/p/7512800.html