HDU 5912 Fraction(模拟——分子式化简求解)
来源:互联网 发布:python 数据类型查询 编辑:程序博客网 时间:2024/04/27 23:56
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5912
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?
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 (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).
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).
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.
You should promise that p/q is irreducible.
Sample Input
1
2
1 1
2 3
Sample Output
Case #1: 1 2
Hint
Here are the details for the first sample: 2/(1+3/1) = 1/2 Source
2016中国大学生程序设计竞赛(长春)-重现赛
题意描述:
给出上述图中的分子式,给出ai和bi
计算该分子形式的最简结果
解题思路:
总体来说还是比较考验数学思维的。
简单实用代入法看一下,例如只有a1,a2和b1,b2,那么结果可以写成b1/(a1+b2/a2)
初始是fz(分子)=b2,fm=a2,经过化简可得结果为b1*fm/(a1*fm+fz),容易得到fz += fm*a[i];和fm *= b[i];只不过fz和fm需要交换一下(仔细验算一下)。
代码实现:
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 }
阅读全文
0 0
- HDU 5912 Fraction(模拟——分子式化简求解)
- HDU 5912 Fraction(模拟——分子式化简求解)
- HDU 5912 Fraction(模拟——分子式化简求解)
- HDU 5912 Fraction 【模拟】
- hdu 5912 Fraction 模拟题
- 分子式的化简
- hdoj 5912 Fraction(模拟)
- 模拟:Fraction HDU
- HDU 5912 Fraction【模拟】(2016中国大学生程序设计竞赛(长春)B题)
- HDU 5912 Fraction 模拟题 【2016中国大学生程序设计竞赛(长春)】
- HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))
- HDU-5912 Fraction(迭代)
- HDU 5912 Fraction
- HDU 5912 Fraction (水题)
- hdu 5912 Fraction
- hdu 5912 Fraction
- HDU-5912-Fraction
- URAL 1468 Fraction(化k进制小数模拟)
- codeforces 494B B. Obsessive String(dp)
- Python 基础个人学习笔记
- 委托_Func委托
- ccf之数字排序(基于Map集合value从大到小排序)
- 算法——程序的灵魂
- HDU 5912 Fraction(模拟——分子式化简求解)
- 洛谷P3275 [SCOI2011]糖果
- 接口和抽象类的联系与区别
- 【函数模板】面向对象程序设计上机练习二(函数模板)
- poi excel导出多次循环导致查询时间过长的解决方案
- SpringBoot拦截器
- 洛谷 1346 电车 spfa
- 【斐波那契数列】51Nod 1350 斐波那契表示
- 学术杂项