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

1
2
1 1
2 3

Sample Output

Case #1: 1 2

Hint

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的更多相关文章

  1. [LeetCode] Fraction to Recurring Decimal 分数转循环小数

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  2. Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  3. 【leetcode】Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  4. Decimal To Fraction 小数转换成分数

    以0.25为例, 0.25 * 100 = 25, 求25 和 100 的最大公约数gcd. 25/gcd 为分子. 100/gcd为分母. //小数转分数 //0.3 -> 3/10, 0.2 ...

  5. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  6. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  7. [LeetCode] Fraction to Recurring Decimal 哈希表

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  8. Java for LeetCode 166 Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  9. LeetCode Fraction to Recurring Decimal

    原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...

  10. 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

随机推荐

  1. mac重置蓝牙模块

    升级系统后蓝牙鼠标无法连接,蓝牙模块也无法关闭,通过重置 PRAM和SMC解决 PRAM 重置 关机,拔掉所有外设,接上电源. 启动时同时按住 Command, Option, p, r , 听到三次 ...

  2. 我对GFWed的一些自己的见解

    首先来听听维基百科对GFWed的说明 防火长城(英语:Great Firewall of China.经常使用简称:GFW,中文也称中国国家防火墙或防火长城[1],中国大陆民众俗称防火墙[2].功夫网 ...

  3. flash画图API:解析obj格式

    又到了周末的时间,依旧的例牌菜.只是近期在和一些同事交流下,学习了一些新的知识.过去一直没有明确的问题,如今总算有点感觉了. 平时编程偶然会用到数学,特别是在做3d的时候.相信看过rokix的3d,那 ...

  4. Git使用笔记2

    工作必备: [更新master] git checkout master git pull git checkout zyb/FirstCommit git merge master //git re ...

  5. MySQL 使用 比较函数 INTERVAL() 函数 实现数据按区间分组

    首先看一下它的定义: INTERVAL(N,N1,N2,N3,..........) INTERVAL()函数进行比较列表(N1,N2,N3等等)中的N值.该函数如果N<N1返回0,如果N< ...

  6. eclipse git拉取完代码后怎么maven构建

    这个参考下:https://jingyan.baidu.com/article/414eccf64d29bc6b431f0ade.html 没试过上面的,反正git后的文件夹非空,用maven创建会失 ...

  7. AIX下RAC搭建 Oracle10G(六)dbca建库

    AIX下RAC搭建系列 AIX下RAC搭建 Oracle10G(六)dbca建库 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 主机名 AI ...

  8. Windows Mobile X图标如何销毁窗体而非隐藏

    在Windows Mobile窗体上,有“OK”和“X”两种形式按钮.1.在Form的属性里,设置“MinimizeBox=false”,则窗体显示”OK”,点击该按钮窗体销毁并退出.2.设置“Min ...

  9. java json字符串和对象互转

    /** * Created by admin on 2017/7/26. */ public class NewPost { private String title; private String ...

  10. Shallow Heap & Retained Heap

    所有包含Heap Profling功能的工具(MAT, Yourkit, JProfiler, TPTP等)都会使用到两个名词,一个是Shallow Size,另一个是 Retained Size. ...