HDU5912 Fraction(模拟)
来源:互联网 发布:怎样申请淘宝企业店铺 编辑:程序博客网 时间:2024/06/05 14:22
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 (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.
Sample Input
121 12 3
Sample Output
Case #1: 1 2
Hint
Here are the details for the first sample: 2/(1+3/1) = 1/2
思路
如图,题目给了从a[1~n]
,b[1~n]
,让你按照图中的方法化简一下式子,最后输出这个式子的最终结果的分子和分母
我们分析一下这个式子,最底下那一个式子可以改写为:
所以我们令初始分母为1,分子为a[n],然后进行模拟。
到了最后会出现这样一种情况,转化一下即可:
代码:
#include<cstdio>#include<cstring>#include<string>#include<set>#include<iostream>#include<stack>#include<queue>#include<vector>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define ll long longusing namespace std;int a[20],b[20];int main(){ int t,n,q=1; scanf("%d",&t); while(t--) { 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 tmp,fz=a[n],fm=1; for(int i=n-1; i>=1; i--) { tmp=a[i]*fz+b[i+1]*fm; fm=fz; fz=tmp; } fm=b[1]*fm; int k=__gcd(fm,fz); printf("Case #%d: %d %d\n",q++,fm/k,fz/k); } return 0;}
- HDU5912 Fraction(模拟)
- HDU5912-Fraction
- HDU5912 Fraction
- HDU5912-Fraction
- HDU5912 Fraction
- hdu5912 Fraction
- HDU5912模拟
- HDU 5912 Fraction 【模拟】
- 模拟:Fraction HDU
- hdu 5912 Fraction 模拟题
- hdoj 5912 Fraction(模拟)
- hdu5914&hdu5912
- CCPC[长春] 2.Fraction 手动模拟
- C语言模拟考|Proper Fraction
- Fraction
- Fraction
- Java 小例子:模拟分数的类 Fraction
- URAL 1468 Fraction(化k进制小数模拟)
- linux文件操作函数(open、write、read、close)可用于串口读写
- js高级语言程序设计笔记——创建对象
- POJ
- 矩阵第一讲
- 日常踩坑--amoeba坑?
- HDU5912 Fraction(模拟)
- 重返母校怀抱,老同学聚会南京大学
- 【肠道生态】-CS0004-肠道菌群相关疾病的最新研究思路及方法
- Python中的条件语句
- Hive简介
- Vue.js介绍
- 肖申克的救赎
- 前端之路——第二篇:认识css
- Struts-2.5 Action动态调用与通配调用无效解决办法