您的位置:首页 > 其它

hdu 5912 Fraction

2017-09-09 20:21 295 查看

Fraction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1274    Accepted Submission(s): 659


[align=left]Problem Description[/align]
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?
 

[align=left]Input[/align]
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≤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).
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

1
2
1 1
2 3

 

[align=left]Sample Output[/align]

Case #1: 1 2

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

 

[align=left]Source[/align]
2016中国大学生程序设计竞赛(长春)-重现赛  

//2016中国大学生程序设计竞赛(长春)-重现赛
//#include"stdafx.h"
#include<iostream>
using namespace std;
#define size 10
int a[size],b[size];
int n;
struct score
{
int fenzi;
int fenmu;
};
score calculate() {
score sum;
sum.fenzi = a
;
sum.fenmu = 1;
for (int i = n; i >= 2; i--) {
int temp = sum.fenmu;
sum.fenmu = sum.fenzi;
sum.fenzi = a[i - 1] * sum.fenzi + b[i] * temp;
}
return sum;
}
int getgcd(int a, int b) {
if (b == 0)
return a;
else
return getgcd(b, a%b);
}
int main() {
int test;
cin >> test;
for (int cases = 1; cases <= test; cases++) {
cin >> n;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
score temp = calculate();
int p = b[1]*temp.fenmu;
int q = temp.fenzi;
int gcd = getgcd(p, q);
cout << "Case #" << cases << ": " << p / gcd << " " << q / gcd << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: