hdu 5912 Fraction -2016中国大学生程序设计竞赛(长春)
来源:互联网 发布:淘宝知识产权侵权标准 编辑:程序博客网 时间:2024/05/27 20:35
Fraction
Problem Description
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?
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 (
The second line contains n integers:
The third line contains n integers:
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
121 12 3
Sample Output
Case #1: 1 2HintHere are the details for the first sample:2/(1+3/1) = 1/2
题目大意:
题目很明白就是求这个公式化简后的分子分母。
解题思路
手动做一下会发现一直在重复做两个步骤:
① a(x-1)+bx/ax;
②把①得到的分子分母交换后分子*b(x-1)
第②步进行完了后会又得到一个分式,然后会重复上面所述步骤。
所以 一个for循环搞定。
注意 求出的结果要化简。
代码:
#include <cstdio>int a[10];int b[10];int gcd(int a, int b) { return b ? gcd(b, a % b) : a;}int main(){ int T; scanf("%d", &T); for (int t = 1; t <= T; t++) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); } int fz = b[n], fm = a[n]; for (int i = n-1; i >= 1; i--) { fz = a[i] * fm + fz; int newfm = fz; fz = b[i] * fm; fm = newfm; } printf("Case #%d: %d %d\n", t, fz / gcd(fz, fm), fm / gcd(fz, fm)); } return 0;}
- hdu 5912 Fraction 2016中国大学生程序设计竞赛(长春) 水题
- hdu 5912 Fraction -2016中国大学生程序设计竞赛(长春)
- HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))
- HDU Fraction 2016中国大学生程序设计竞赛(长春)-重现赛
- HDU 5912 Fraction【模拟】(2016中国大学生程序设计竞赛(长春)B题)
- HDU 5912 Fraction 模拟题 【2016中国大学生程序设计竞赛(长春)】
- 2016中国大学生程序设计竞赛(长春)1002 Fraction 数学
- 2016中国大学生程序设计竞赛(ccpc 长春) Fraction【模拟】
- HDU Triangle 2016中国大学生程序设计竞赛(长春)-重现赛
- HDU 5912 【模拟,类连分数的计算】【2016中国大学生程序设计竞赛(长春)】
- HDU 5914 Triangle 【构造】 (2016中国大学生程序设计竞赛(长春))
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
- hdu 5914 Triangle 2016中国大学生程序设计竞赛(长春)(脑洞题)
- HDU 5918 Sequence I【暴力+剪枝】(2016中国大学生程序设计竞赛(长春)H题)
- HDU Harmonic Value Description 2016中国大学生程序设计竞赛(长春)-重现赛
- HDU Sequence I 2016中国大学生程序设计竞赛(长春)-重现赛
- HDU Ugly Problem 2016中国大学生程序设计竞赛(长春)-重现赛
- HDU 5914 Triangle【找规律】【2016中国大学生程序设计竞赛(长春)】
- 二叉树遍历
- 重装系统U盘版本
- JavaScript实现tab标签-解决循环bug问题
- EM算法学习笔记
- Android简易实战教程--第三十八话《自定义通知NotifiCation》
- hdu 5912 Fraction -2016中国大学生程序设计竞赛(长春)
- 面试经历
- 【egret】 egret游戏中添加“长按识别二维码”
- MAC 下修改mysql默认密码方法
- SRP Protocol Design
- 对N行N列二维数组的每一行排序,偶数行(0当作偶数)由小到大排序,奇数行由大到小排序
- C++顺序容器
- 《C程序设计》
- 初老的苦涩