Fraction
来源:互联网 发布:poser pro 是什么软件 编辑:程序博客网 时间:2024/05/29 07:17
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?
For each test case, the first line contains only one integer n (
The second line contains n integers:
The third line contains n integers:
You should promise that p/q is irreducible.
121 12 3
Case #1: 1 2
Here are the details for the first sample:2/(1+3/1) = 1/2
就是按照按照图片上的一步一步的计算。从后往前推。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[1000],b[1000];int grc(int a,int b){ if(a%b) return grc(b,a%b); else return b;}int main(){ int kk=1; int t,n,m,i,j; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } for(i=1;i<=n;i++) { scanf("%d",&b[i]); } int q,p,k; p=a[n];q=b[n]; for(i=n-1;i>0;i--) { k=p; p=p*a[i]+q; q=k*b[i]; } int m=grc(p,q); printf("Case #%d: ",kk); printf("%d %d\n",q/m,p/m); kk++; } return 0;}
阅读全文
0 0
- Fraction
- Fraction
- POJ1930Dead Fraction
- XTU1236:Fraction
- proper fraction
- XTU1236 Fraction
- HDU5912-Fraction
- HDU5912 Fraction
- HDU5912-Fraction
- HDU5912 Fraction
- hdu5912 Fraction
- A. Fraction
- POJ 1930 Dead Fraction
- Continued Fraction of Squareroot
- POJ_1980_Unit Fraction Partition
- SZU:G32 Mass fraction
- POJ-1930-Dead Fraction
- An Irreducible Fraction
- lintcode刷题——格雷编码
- 非递归实现二叉树遍历
- 嵌入式Linux应用学习(二)------依赖Linux kernel驱动的uart串口应用程序编写
- sklearn(三)------模型的属性和用法
- 集合框架
- Fraction
- qsort double型排序
- Java--正则表达式基础入门(三)
- 515_EventBus使用
- 安卓多用户笔记-用户创建
- windows下redis的安装
- linux中的邮箱配置
- 《华为机试在线训练》之删除字符串中出现次数最少的字符
- 常用的排序算法