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 ...
随机推荐
- DEB方式在UBUNTU安装ODOO 8.0
odoo在ubuntu最简单最快速安装方式是deb方式,基本无需再去改数据库配置文件,全自动化了,odoo中文网推荐新手采用此方法 1 安装数据库:sudo apt-get install postg ...
- STL源码剖析(空间配置器)
前言 在STL中,容器的定义中都带一个模板参数,如vector template <class T, class Alloc = alloc> class vector {...} 其中第 ...
- <转>C++位运算详解
原文转自:http://www.crazycpp.com/?p=82 前言 以前收藏过一篇讲C++位操作的文章,这次博客搬家,以前的数据都没有保留,整理谷歌网站管理后台的时候,发现不时的还有网友有在查 ...
- How to manually remove an infected file from your computer
http://blog.csdn.net/pipisorry/article/details/41258577 How to manually remove an infected file from ...
- redis实践一些要注意的事项
不要放垃圾数据,及时清理无用数据实验性的数据和下线的业务数据及时删除; key尽量都设置过期时间对具有时效性的key设置过期时间,通过redis自身的过期key清理策略来降低过期key对于内存的占用, ...
- C++ 内存泄漏
1. 内存泄漏: 在计算机科学中,内存泄漏指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况. 内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前 ...
- 很实用的JQuery代码片段(转)
1 元素屏幕居中 jQuery.fn.center = function () { this.css("position","absolute"); this. ...
- Atitit.跨语言反射api 兼容性提升与增强 java c#。Net php js
Atitit.跨语言反射api 兼容性提升与增强 java c#.Net php js 1. 什么是反射1 1.1. 反射提供的主要功能:1 1.2. 实现反射的过程:1 ...
- RTT常用数据类型
RTT常用数据类型定义在rtdef.h中 /* RT-Thread basic data type definitions */ typedef signed char rt_int8_t; /**& ...
- Java基础12 类型转换与多态(转载)
类型检查 Java的任意变量和引用经过类型声明(type declaration),才能使用.我们之前见过对象数据.类数据.方法参数.方法返回值以及方法内部的自动变量,它们都需要声明其类型.Java是 ...