UVA 12068 - Harmonic Mean Solution
Solve in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
long long n,ar[1000]= {0},i,m1=1,n1=0,k,t,m=0,r,r1;
cin>>t;
for(k=1; k<=t; k++)
{
cin>>n;
for(i=0; i<n; i++)
{
cin>>ar[i];
m1=m1*ar[i];
}
m=m1*n;
for(i=0; i<n; i++)
{
n1=n1+(m1/ar[i]);
}
m1=gcd(m,n1);
if(m1==1)
printf("Case %lld: %lld/%lld\n",k,m,n1);
else
{
r=m/m1;
r1=n1/m1;
printf("Case %lld: %lld/%lld\n",k,r,r1);
}
m=0,n1=0,m1=1,r=0,r1=0;
}
return 0;
}
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
long long n,ar[1000]= {0},i,m1=1,n1=0,k,t,m=0,r,r1;
cin>>t;
for(k=1; k<=t; k++)
{
cin>>n;
for(i=0; i<n; i++)
{
cin>>ar[i];
m1=m1*ar[i];
}
m=m1*n;
for(i=0; i<n; i++)
{
n1=n1+(m1/ar[i]);
}
m1=gcd(m,n1);
if(m1==1)
printf("Case %lld: %lld/%lld\n",k,m,n1);
else
{
r=m/m1;
r1=n1/m1;
printf("Case %lld: %lld/%lld\n",k,r,r1);
}
m=0,n1=0,m1=1,r=0,r1=0;
}
return 0;
}
No comments