Fraction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1010 Accepted Submission(s): 532
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
Sample Output
Case #1: 1 2
Here are the details for the first sample: 2/(1+3/1) = 1/2
//题意很容易理解,就是求出这样的式子的分子,分母最简形式
模拟一下即可
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
#define MX 105
int n;
int A[MX];
int B[MX]; int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
} int main()
{
int T;
scanf("%d",&T);
for (int cnt=;cnt<=T;cnt++)
{
scanf("%d",&n);
for (int i=;i<=n;i++)
scanf("%d",&A[i]);
for (int i=;i<=n;i++)
scanf("%d",&B[i]);
int p=B[n],q=A[n];
int a,b;
for (int i=n-;i>=;i--)
{
a = A[i],b = B[i];
a = a*q + p;
b = b*q; p = b ;
q = a;
}
int yue = gcd(p,q);
printf("Case #%d: %d %d\n",cnt,p/yue,q/yue);
}
return ;
}
Fraction的更多相关文章
- [LeetCode] Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- Decimal To Fraction 小数转换成分数
以0.25为例, 0.25 * 100 = 25, 求25 和 100 的最大公约数gcd. 25/gcd 为分子. 100/gcd为分母. //小数转分数 //0.3 -> 3/10, 0.2 ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- [LeetCode] Fraction to Recurring Decimal 哈希表
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- LeetCode Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...
- 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
随机推荐
- POJ Cow Exhibition
题目链接:Click Here~ 题目意思自己看吧. 算法分析: 对我来想是没有想到,最后看别人的博客才知道的.要把当中的一个条件当作体积.由于两个条件都存在负数,所以还要先保证最后不会再体积中出现负 ...
- 火车票订票API 用PHP完成火车票订票流程
本教程用来演示聚合数据-火车票订票接口的使用流程. 配置好PHP环境,PHP版本最好大于5.5 去聚合数据-火车票订票接口申请key:http://www.juhe.cn/docs/api/id/17 ...
- GDB基本命令(整合)(转)
directory:添加源文件目录 l src.cpp:line_num可进入文件 如:l src.cpp:10 回车自动重复上一命令 一.gdb调试基本知识a.调试器指示的是将要执行的代码行b.只有 ...
- 转:nolock的替代方案-提交读快照隔离[行版本控制]
with(nolock)并意味着没有锁,实际上在查询一张表时,还是有锁,会对对象增加架构锁, 防止表会修改,会对数据库增加共享锁.若使用drop index,则要等到架构锁释放. sql serv ...
- where 泛型类型参数及约束
private void InsertData<TRowMetadata, TFieldMetadata, TCellMetadata>(IMetadataReader<TRowMe ...
- TADOConnection如何测试连接数据库超时的解决办法
TADOConnection如何测试连接数据库超时的解决办法 TADOConnection 提供了很好的数据库连接功能,但是我们在实际使用时,如果指定的远程数据库不存在或其他原因连接不上,程序往往会 ...
- WIN10系统如何设置系统的护眼色
小编的眼神不是很好,所以喜欢把电脑的各种文档的背景色设置成护眼色(就是在视觉效果看起来非常舒适的那种淡绿色) 方法/步骤 windows+R键调出运行窗口(或者鼠标右击开始键,选择运行) ...
- 【应用篇】Activiti外置表单实例demo(四)
在这里我想说的外置表单.是说我们将我们自己的jsp(.form,.html)等页面上传到工作流的数据库中,当任务运行到当前结点时.给我们像前台发送绑定好的表单. 此处是给表单绑定表单的过程 water ...
- win7安装RabbitMQ
1.下载并安装erlang http://www.erlang.org/downloads 2.下载并安装RabbitMQ http://www.rabbitmq.com/install-window ...
- OC-7-内存管理
课程要点: 内存管理的必要性 MRC(手动管理) 自动释放池 ARC是怎么对内存进行管理的 内存管理的必要性 OC是一门面向对象的语言,在软件运行过程中会创造大量的对象,每创建一个对象系统就会给其分配 ...