Search Here

Tuesday, June 30, 2015

UVa - 12068 ( Harmonic Mean Solution ) [ C & C ++ Code ]

Try Yourself First. For help Scroll Down .

Clearance : In this problem you have t Find the Harmonic Mean of the given numbers according to the Equation given . But you can't take / make any Floating point numbers to solve this problem . One more point you have to Find the Result in a DIVISION format like A/B . To do that you have to divide the pre-resultant A & B with such a number that the result of Division for both A & B will 1 . Suppose ,   90/25 = 18/5 . Hope Got That .

Tip : To find such a number that is divisible by Both A & B you can use GCD becuse . GCD is the most efficient number that will Find you the Greatest Number that will be divisible by Both A & B .Careful about the output format .

Notes :  In this code , you will found a New thing . I have used a Template for find  out GCD . Its not needed to do that with GCD . But I did it for my own Pleasure . You can solve it with any GCD Function .  You can solve this Problem with C language by creating a GCD user defined function and removing C++ headers . All those this except These are alike are a C code .

Code :


#include<bits/stdc++.h>

template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }

using namespace std;

int main()
{
    int test, caseno=0;
    scanf("%d",&test);
    while(test--)
    {
        int n;
        scanf("%d",&n);
        int a[n+1];
        long long int dot = 1, sum = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            dot = dot * a[i];
        }
        for(int i=0; i<n; i++)
            sum = sum + (dot/a[i]);
        dot = n * dot;
        long long int res1 = dot/(gcd<long long int>(dot,sum));
        long long int res2 = sum/(gcd<long long int>(dot,sum));
        printf("Case %d: %lld/%lld\n",++caseno, res1, res2);
    }
    return 0;
}

=> Question ?? Leave a Comment .

No comments:

Post a Comment