• 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;
    }
  • 相关阅读:
    lambda表达式
    netstat
    【makfile | 资源】网址链接
    【makefile】 $@ $^ %< 的使用

    【顺序容器 || 09】
    标准IO库
    我使出这“三板斧”(分段锁、哈希锁、弱引用锁)灭霸跑了......
    JAVA 线上故障排查完整套路,从 CPU、磁盘、内存、网络、GC 一条龙!
    一条 SQL 引发的事故,同事直接被开除!
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5931382.html
Copyright © 2020-2023  润新知