HDU 5912 【模拟,类连分数的计算】【2016中国大学生程序设计竞赛(长春)】

Fraction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 631    Accepted Submission(s): 355


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 ( n8 ).

The second line contains n integers:  a1,a2,an(1ai10 ).
The third line contains n integers:  b1,b2,,bn(1bi10) .
 

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
 

Source

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5912


输入如图所示得到分数,计算其值。

模拟题也要技巧。

Matrix67: The Aha Moments 连分数:


2016中国大学生程序设计竞赛(长春)Rank:http://board.acmicpc.info/ccpc2016/jlu_onsite.php


AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[10];
int b[10];
int GCD(int x,int y)
{
    return y==0?x:GCD(y,x%y);
}
int main()
{
    int T,kase=0;
    //freopen("data/5912.txt","r",stdin);
    cin>>T;
    int n;
    while(T--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=n;i++)
            cin>>b[i];
        int fz=b[n],fm=a[n];
        for(int i=n-1;i>0;i--)
        {
            fz+=a[i]*fm;
            fm*=b[i];
            swap(fz,fm);
        }
        int gcd=GCD(fz,fm);
        fz/=gcd,fm/=gcd;
        printf("Case #%d: %d %d\n",++kase,fz,fm);
    }
    return 0;
}

尊重原创,转载请注明出处: http://blog.csdn.net/hurmishine



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# ContinuedFraction #### 项目介绍 连分数计算器 支持连分数和小数输入,高精度小数转连分数,无精度损失,用于获取小数在一定范围内最接近的分数 例如π的高精度转连分数 str=> 3.14159265358979 num=> 3.14159265358979000000000000000000000 ctf=> [3;7,15,1,292,1,1,1,2,1,3,1,12,2,4,1,1,3,2,2,1,18,1,2,2,1,7,2,2] 1=> 3.00000000000000000000000000000000000 3 3/1 2=> 3.14285714285714285714285714285714286 7 22/7 3=> 3.14150943396226415094339622641509434 15 333/106 4=> 3.14159292035398230088495575221238938 1 355/113 5=> 3.14159265301190260407226149477372968 292 103993/33102 6=> 3.14159265392142104470871594159265392 1 104348/33215 7=> 3.14159265346743670552045478534915632 1 208341/66317 8=> 3.14159265361893662339750030141060162 1 312689/99532 9=> 3.14159265358107777120441930658185778 2 833719/265381 10=> 3.14159265359140397848254241421927966 1 1146408/364913 11=> 3.14159265358938917154368732170690821 3 4272943/1360120 12=> 3.14159265358981538324194377730744861 1 5419351/1725033 13=> 3.14159265358978910556761228975786423 12 69305155/22060516 14=> 3.14159265358979009430798477470203822 2 144029661/45846065 15=> 3.14159265358978998813773682909318658 4 645423799/205444776 16=> 3.14159265358979000750767514045607416 1 789453460/251290841 17=> 3.14159265358978999879486079142367388 1 1434877259/456735617 18=> 3.14159265358979000014512509093352444 3 5094085237/1621497692 19=> 3.14159265358978999997843356720301190 2 11623047733/3699731001 20=> 3.14159265358979000000839600248412328 2 28340180703/9020959694 21=> 3.14159265358978999999968162106153623 1 39963228436/12720690695 22=> 3.14159265358979000000001193310441815 18 747678292551/237993392204 23=> 3.14159265358978999999999517378526962 1 787641520987/250714082899 24=> 3.14159265358979000000000056801156993 2 2322961334525/739421558002 25=> 3.14159265358978999999999978607241192 2 5433564190037/1729557198903 26=> 3.14159265358979000000000002025128805 1 7756525524562/2468978756905 27=> 3.14159265358978999999999999894805542 7 59729242861971/19012408497238 28=> 3.14159265358979000000000000024695141 2 127215011248504/40493795751381 29=> 3.14159265358979000000000000000000000 2 314159265358979/100000000000000

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值