HDU - 5912——Fraction

简介: HDU - 5912——Fraction

题目:

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:

20190823100250426.png

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<stdio.h>
#include<string.h>
int a[200],b[200];
int f(int c,int d)//求最大公因数的递归
{
  if(d==0)
    return c;
  else
    return f(d,c%d);
}
int main()
{
  int i,j,k,n,m,cas=1,p,q,t1,t2,t3;
  int N;
  scanf("%d",&N);
  while(N--)
  {
    scanf("%d",&n);
    for(i=1;i<=n;i++)
      scanf("%d",&a[i]);
    for(i=1;i<=n;i++)
      scanf("%d",&b[i]);
    t1=b[n];
    t2=a[n];
    for(i=n;i>=2;i--)//从下往上一直到倒数第二个
    {
      m=t2;
      t2=a[i-1]*t2+t1;
      t1=m*b[i-1];
    }
    p=f(t1,t2); 
    printf("Case #%d: %d %d\n",cas++,t1/p,t2/p);
  }
  return 0;
}

相关文章
HDU 2549 壮志难酬
壮志难酬 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12541    Accepted Submission(s): 4166 Problem Description 话说MCA山上各路豪杰均出山抗敌,去年曾在江湖威名显赫的,江湖人称的甘露也不甘示弱,“天将降大任于斯人也,必先劳其筋骨,饿其体肤,空乏其身”他说。
1002 0
|
定位技术
hdu 4771 Stealing Harry Potter's Precious
点击打开链接 题意:题目给定一个n*m的地图,地图有一个起点标记为'@',还有'#'表示不能够走的,'.'表示可以走。给定k个点,问从起点开始把这k个点走过去的最小步数。
772 0
|
人工智能
hdu2084数塔
经典问题了,题意我就不叙述了(题目是中文的~) 分析:dp[i][j]表示在第i行第j个位置上能取得的最大和,那么要从最后一行开始算起,到塔顶结束:dp[i][j] = a[i][j]+max(dp[i+1][j], dp[i+1][j+1]), 边界条件是dp[n][j] = a[n][j]; ...
649 0
|
存储
hdu 2795 Billboard
点击打开hdu 2795 思路: 线段树+单点更新 分析: 1 题目的意思是给定一个h*w的广告牌h为高,w为宽,现在有n个高为1宽为wi的小广告要放上去,原则是最先放最上面和最左边的位置 2 题目的h和w的最大值为10^9,但是n最大为200000。
746 0
|
算法 BI 人工智能
hdu 1217 Arbitrage
点击打开链接hdu 1217 思路:最短路变形题(floyd 或 SPFA) 分析: 2 题目要求的是经过一轮的转换之后,原来的比例能够大于1。
878 0