zoukankan      html  css  js  c++  java
  • hdu 5912(迭代+gcd)

    Fraction

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


    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
     
    题意:求图上的公式的结果.直接迭代求然后约分
    #include <bits/stdc++.h>
    using namespace std;
    map<int ,int> value;
    int a[10],b[10];
    int gcd(int x,int y){
        return y==0?x:gcd(y,x%y);
    }
    int main()
    {
        int tcase,t=1;
        scanf("%d",&tcase);
        while(tcase--){
            int n,res1,res2;
            scanf("%d",&n);
            for(int i=1;i<=n;i++) scanf("%d",&a[i]);
            for(int i=1;i<=n;i++) scanf("%d",&b[i]);
            res1 = a[n],res2 = b[n];
            for(int i=n-1;i>=1;i--){
                int k = res1;
                res1 = a[i]*res1+res2;
                res2 = b[i]*k;
            }
            int d = gcd(res1,res2);
            printf("Case #%d: %d %d
    ",t++,res2/d,res1/d);
        }
        return 0;
    }
  • 相关阅读:
    php require与include的区别
    php页面消耗内存过大处理办法
    flex if(a is b){},flex if(a as b){} 意思
    thinkphp的配置文件修改后不起作用
    ThinkPHP 入口文件定义 3.0 输出两次解决办法
    JS中的Math.ceil和Math.floor函数
    ThinkPHP 配置问题
    Google 快照无法使用时,如何恢复
    首页被收录了,但是只显示网址,处理办法
    RED5 端口使用
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5931382.html
Copyright © 2011-2022 走看看