12068 - Harmonic Mean

All about problems in Volume 120. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
esharif
New poster
Posts: 18
Joined: Sun Jun 03, 2012 11:56 pm

12068 - Harmonic Mean

Post by esharif » Sat Jul 07, 2012 5:09 pm

:oops: I'm getting WA, but why? plz suggest me and provide me some critical i/p & o/p
my code just like below:

Code: Select all

#include<stdio.h>

int main()
{
    int s, j, i, n, a[1000];
    long long int mul, sum, lob, hor;
    while(scanf("%d", &s)==1)
    {
        for(i=1; i<=s; i++)
        {
            mul=1;
            sum=0;
            scanf("%d", &n);
            for(j=0; j<n; j++)
            {
                scanf("%d", &a[j]);
                mul*=a[j];
            }
            for(j=0; j<n; j++)
            {
                sum+=(mul/a[j]);
            }
            lob=(long long int)mul*n, hor=sum;
            while(1)
            {
                if(lob%2==0 && hor%2==0)
                {
                    lob/=2, hor/=2;
                    continue;
                }
                else if(lob%3==0 && hor%3==0)
                {
                    lob/=3, hor/=3;
                    continue;
                }
                else if(lob%5==0 && hor%5==0)
                {
                    lob/=5, hor/=5;
                    continue;
                }
                else if(lob%7==0 && hor%7==0)
                {
                    lob/=7, hor/=7;
                    continue;
                }
                else
                    break;
            }
            printf("Case %d: %lld/%lld\n", i, lob, hor);
        }
    }
    return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Why 12068 is WA, help me plz...

Post by brianfry713 » Tue Jul 17, 2012 10:43 pm

What if the numerator and denominator are both divisible by 11? It would be faster to just divide by the GCD.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 120 (12000-12099)”