2011-12-03 19:20:14Morris
[UVA] 12068 - Harmonic Mean
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 mn
. 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;
}