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;
}
}
相关文章推荐
- hdu 5912 Fraction
- HDU 5912 Fraction 模拟题 【2016中国大学生程序设计竞赛(长春)】
- HDU 5912 Fraction (2016-ccpc-长春)
- hdu 5912 Fraction
- hdu 5912Fraction
- HDU 5912 Fraction【模拟】(2016中国大学生程序设计竞赛(长春)B题)
- HDU 5912 Fraction(模拟——分子式化简求解)
- HDU 5912 Fraction(模拟——分子式化简求解)
- HDU 5912 Fraction
- HDU-5912 Fraction(迭代)
- HDU 5912 Fraction(模拟——分子式化简求解)
- hdu 5912 Fractionl 2016ACM/CCPC长春赛区现场赛B
- hdu 5912 Fraction -2016中国大学生程序设计竞赛(长春)
- 模拟:Fraction HDU - 5912
- hdu 5912 Fraction 2016中国大学生程序设计竞赛(长春) 水题
- HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))
- hdu 5912 Fraction(除法迭代)
- HDU-5912-Fraction
- HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))
- HDU 5912 Fraction 【模拟】