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 ...
随机推荐
- CSDN 夏令营程序 试题分析 (3)
首先大家先来看题目: 分析: 三维数组存储以行为主序列,计算公式例如以下: Loc(Ai,j,k)=Loc(Ac1c2c3)+[(i-c1)V2V3+(j-c2)V3+(k-c3)]*L 当中c1.c ...
- Java实现图片裁剪预览功能
在项目中.我们须要做些类似头像上传,图片裁剪的功能,ok看以下文章! 须要插件:jQuery Jcrop 后端代码: package org.csg.upload; import java.awt.R ...
- 《Docker 入门与实践》 已经出版了~欢迎有须要的朋友关注。
在云计算时代.开发人员将应用转移到云上已经攻克了硬件管理的问题,然而软件配置和管理相关的问题依旧存在. Docker的出现正好能帮助软件开发人员开阔思路.尝试新的软件管理方法来解决问题. 通过掌握Do ...
- Eclipse githup
1:选择对应eclipse版本的githup组件版本,详见网址: http://wiki.eclipse.org/EGit/FAQ#Where_can_I_find_older_releases_of ...
- 切换默认Activity和Fragment的动画
Activity中 public void click(View view){ Intent intent = new Intent(); intent.setClass(this, TwoActiv ...
- How to set Selenium Python WebDriver default timeout?
Trying to find a good way to set a maximum time limit for command execution latency in Selenium Pyth ...
- ScrollView嵌套ListView冲突问题的最优解决方式
项目做多了之后.会发现事实上ScrollView嵌套ListVew或者GridView等非经常常使用,可是你也会发现各种奇怪问题产生.依据个人经验如今列出常见问题以及代码最少最简单的解决方法. 问题一 ...
- linux使用fdisk命令操作硬盘
知识点: MBR:Master Boot Record 主引导记录 在硬盘0柱面 0磁头的第一个扇区,占512字节(3部分 主引导程序 446字节,硬盘分区表DPT[disk partion tabl ...
- Atitit.播放系统规划新版本 and 最近版本回顾 v3 pbf.doc
Atitit.播放系统规划新版本 and 最近版本回顾 v3 pbf.doc 1 版本11 (ing)4 1.1 规划h5本地缓存系列 4 1.2 Android版本app4 1.3 双类别系统, ...
- imx6用文件io操作gpio
具体请参考: http://blog.csdn.net/u014213012/article/details/53140781 这里要注意的是: 要让linux支持文件io方式操作gpio,首先驱动必 ...