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;
    }
  • 相关阅读:
    hdu4665 DFS
    hdu4665 DFS
    hdu4717 三分(散点的移动)
    POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈
    洛谷 P2347 砝码称重
    洛谷 P3009 [USACO11JAN]利润Profits
    洛谷 P2925 [USACO08DEC]干草出售Hay For Sale
    洛谷 P1616 疯狂的采药
    洛谷 P1086 花生采摘
    洛谷 P1048 采药
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5931382.html
Copyright © 2011-2022 走看看