24h購物| | PChome| 登入
2011-12-03 19:20:14| 人氣750| 回應0 | 上一篇 | 下一篇

[UVA] 12068 - Harmonic Mean

推薦 0 收藏 0 轉貼0 訂閱站台

Output 

For each set of input produce one line of output. This line contains the serial of output followed by two integers m and n separated by a front slash. These two numbers actually indicate that the harmonic mean of the given four numbers is m$ over$n . You must ensure that gcd(m, n) = 1 or in other words m and n must be relative prime. The value of m and n will fit into a 64-bit signed integer.

Sample Input 

2
4 1 2 3 4
4 2 2 3 1

Sample Output 

Case 1: 48/25
Case 2: 12/7




#include<stdio.h>
long long gcd(long long x, long long y) {
    long long t;
    while(x%y) {
        t = x, x = y, y = t%y;
    }
    return y;
}
int main() {
    int T, C = 0, i, N;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &N);
        long long A[10], m = 0, n = 0, tmp = 1;
        for(i = 0; i < N; i++)
            scanf("%lld", &A[i]), tmp *= A[i];
        m = tmp*N;
        for(i = 0; i < N; i++)
            n += tmp/A[i];
        tmp = gcd(m, n), m /= tmp, n /= tmp;
        printf("Case %d: %lld/%lld\n", ++C, m, n);
    }
    return 0;
}

台長: Morris
人氣(750) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 12195 - Jingle Composing
此分類上一篇:[UVA] 11942 - Lumberjack Sequencing

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文