You are not logged in. Please login at www.codechef.com to post your questions!

×

WA in COOLING

Following is the code I used for the problem Cooling Pies(http://www.codechef.com/problems/COOLING)> Itried many test cases but when I run the judge it says wrong answer please help.

include<iostream>

include<stdlib.h>

int compare (const void * a, const void * b);
int main()
{
int a[30]={0},b[30]={0},cases,num,ans=0,x=0;
scanf("%d",&cases);
for(int i=0;i<cases;i++)
{
    scanf("%d",&num);
    for(int j=0;j<num;j++)
    {
        scanf("%d",&a[j]);
    }
    for(int k=0;k<num;k++)
    {
        scanf("%d",&b[k]);
    }
    qsort(a,30,sizeof(int),compare);
    qsort(b,30,sizeof(int),compare);
    for(int l=0;l<num;l++)
    {
        if(b[x]>=a[l])
        {
            ans+=1;
            x+=1;
        }
    }
    printf("%d\n",ans);
}
}
int compare (const void * a, const void * b)
{
  return ( *(int*)b - *(int*)a );
}

asked 08 Feb '13, 19:02

shikhs123's gravatar image

3★shikhs123
163811
accept rate: 0%


dude initialise ans and x to zero for every new test case .... you have done that on top only

link

answered 08 Feb '13, 19:52

amitupadhyay's gravatar image

2★amitupadhyay
1.4k92241
accept rate: 14%

1

your statement ans=0 and x=0 will be inside for loop.

further more use #include<cstdio> for scanf n printf

also #include<cstdlib> is correct header...

plus qsort((void*)a,30,sizeof(int),compare); typecast it to be on safer side :).... hope that will help..

(08 Feb '13, 19:55) amitupadhyay2★

a very detailed and informative answer for one of the most silliest mistake I committed once again.thanx a lot for helping me :)

(08 Feb '13, 22:14) shikhs1233★
1

my pleasure :)

(09 Feb '13, 13:46) amitupadhyay2★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×3,707
×1,901
×1,056
×29

question asked: 08 Feb '13, 19:02

question was seen: 860 times

last updated: 09 Feb '13, 13:46