HDU 5912 Fraction 模拟 (2016中国大学生程序设计竞赛(长春))
Posted Coolxxx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5912 Fraction 模拟 (2016中国大学生程序设计竞赛(长春))相关的知识,希望对你有一定的参考价值。
Fraction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0Problem DescriptionMr. 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?
InputThe 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: a1,a2,⋯an(1≤ai≤10).
The third line contains n integers: b1,b2,⋯,bn(1≤bi≤10).
OutputFor 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 Input1 2 1 1 2 3
Sample OutputCase #1: 1 2HintHere are the details for the first sample: 2/(1+3/1) = 1/2
Statistic | Submit | Clarifications | Back
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5912
题目大意:
给你一个分式,如图,求化简后的分子分母(不含公约数)。
题目思路:
【模拟】
因为n只有10,可以直接模拟。
1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<stack> 10 #include<queue> 11 #include<set> 12 #include<bitset> 13 #include<memory.h> 14 #include<time.h> 15 #include<stdio.h> 16 #include<stdlib.h> 17 #include<string.h> 18 //#include<stdbool.h> 19 #include<math.h> 20 #define min(a,b) ((a)<(b)?(a):(b)) 21 #define max(a,b) ((a)>(b)?(a):(b)) 22 #define abs(a) ((a)>0?(a):(-(a))) 23 #define lowbit(a) (a&(-a)) 24 #define sqr(a) ((a)*(a)) 25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 26 #define mem(a,b) memset(a,b,sizeof(a)) 27 #define eps (1e-10) 28 #define J 10 29 #define mod 1000000007 30 #define MAX 0x7f7f7f7f 31 #define PI 3.14159265358979323 32 #pragma comment(linker,"/STACK:1024000000,1024000000") 33 #define N 2004 34 using namespace std; 35 typedef long long LL; 36 double anss; 37 LL aans,sum; 38 int cas,cass; 39 int n,m,lll,ans; 40 int a[N],b[N]; 41 int gcd(int a,int b) 42 { 43 if(!b)return a; 44 return gcd(b,a%b); 45 } 46 int main() 47 { 48 #ifndef ONLINE_JUDGEW 49 // freopen("1.txt","r",stdin); 50 // freopen("2.txt","w",stdout); 51 #endif 52 int i,j,k; 53 // init(); 54 // for(scanf("%d",&cass);cass;cass--) 55 for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 56 // while(~scanf("%s",s)) 57 // while(~scanf("%d",&n)) 58 { 59 scanf("%d",&n); 60 for(i=1;i<=n;i++)scanf("%d",&a[i]); 61 for(i=1;i<=n;i++)scanf("%d",&b[i]); 62 int fz=b[n],fm=a[n]; 63 for(i=n-1;i;i--) 64 { 65 fz+=a[i]*fm; 66 fm*=b[i]; 67 swap(fz,fm); 68 } 69 i=gcd(fz,fm); 70 fz/=i,fm/=i; 71 printf("Case #%d: ",cass); 72 printf("%d %d\\n",fz,fm); 73 } 74 return 0; 75 } 76 /* 77 // 78 79 // 80 */
以上是关于HDU 5912 Fraction 模拟 (2016中国大学生程序设计竞赛(长春))的主要内容,如果未能解决你的问题,请参考以下文章
CCPC2016长春B (hdu 5912 Fraction)
HDU 6223 Infinite Fraction Path(BFS+剪枝)
hdu 6223 Infinite Fraction Path