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 ...
随机推荐
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何获取标准驱动器扭矩值获取电流值
双击某个驱动器(以松下伺服驱动器为例),在Process Data中,注意默认显示了PDO mapping1的数据(Error code, status word等) 注意左侧,2和3分别表示了与 ...
- Linux学习笔记 (七)挂载命令
在Linux中,光盘,U盘,硬盘在使用之前必须进行挂载,挂载类似windows中的分配盘符. 一.查看挂载和自动挂载 1.mount:直接输入mount表示查看系统中所有的挂载点. 2.mount - ...
- Linux-进程内存占用情况
可以直接使用top命令后,查看%MEM的内容.可以选择按进程查看或者按用户查看,如想查看oracle用户的进程内存使用情况的话可以使用如下的命令: (1)top top命令是Linux下常用的性能分析 ...
- 你被美国监控了,美国监控丑闻——"棱镜项目"事件
http://www.ittime.com.cn/index.php?m=content&c=index&a=show&catid=29&id=3795 “棱镜”项目所 ...
- linux 网络性能优化
最近在对程序进行调优,涉及到了网络通信,学习了一下对网络调优的方法,网上的资料很多,针对软件优化方面,大体上主要有两种方式:一是网卡参数,另一个是内核参数. 一. 网卡参数优化 针对网卡参数优化,需要 ...
- LeetCode 66 Plus One(加一)(vector)
翻译 给定一个以一系列数字表示的非负数.将其加一并转换成数字. 数字存储的最高位在列的最前面. 原文 Given a non-negative number represented as an arr ...
- javascript执行环境以及作用域链的理解
在javascript脚步语言中执行环境有两种: 全局环境: 局部环境: 我们可以拿一个田径跑道来打比方,全局环境就可以理解为是最外面跑道,它包含着内部所有的东西,有人在跑步,有人在跳远,这些用着不同 ...
- windows system.exe占用文件
1)问题的原因是出于一个服务Application Experience,如果装好系统后就把此服务设为手动启动了,平时运行也没什么异常.但是win7在运行exe时如果没有这个服务的辅助就会长时间的占用 ...
- awk 截取字符串
1.把字符串的变量存入到其他变量中 1.1.编辑 shell 文件 [root@m910-120 test]# vi awkTest.sh ips=10.0.204.217:10.0.204.218 ...
- out传值
public void Out(out int a, out int b) {//out相当于return返回值 //可以返回多个值 //拿过 ...