1

I have the following code.

CodeBlocks outputs NO for the following input, but Codeforces outputs YES?

For the following input:

1500 1500 1500 1 2 3, the right output is NO (correct in CodeBlocks), but it outputs YES with the Codeforces "compiler" (aka custom invocation).

I've tried everything and I'm very much out of ideas.

#include <stdio.h>
#include <string.h>

int main()
{
int andrewGrapes, dmitryGrapes, michalGrapes;
int greenGrapes, purpleGrapes, blackGrapes;
int andrewCheck, dmitryCheck, michalCheck;

// how many grapes they want to eat
scanf("%d", &andrewGrapes); // green grapes only
scanf("%d", &dmitryGrapes); // purple and green
scanf("%d", &michalGrapes); // any grapes

// number of grapes
scanf("%d", &greenGrapes);
scanf("%d", &purpleGrapes);
scanf("%d", &blackGrapes);

if (greenGrapes >= andrewGrapes)
{
    andrewCheck = 1;
    greenGrapes = greenGrapes - andrewGrapes;
}

if (greenGrapes + purpleGrapes >= dmitryGrapes)
{
    greenGrapes = greenGrapes - dmitryGrapes;
    purpleGrapes = purpleGrapes - dmitryGrapes;

    dmitryCheck = 1;
}

//if (greenGrapes + purpleGrapes + blackGrapes >= michalGrapes)
  //  michalCheck = 1;

if (andrewCheck == 1)
{
    if (dmitryCheck == 1)
    printf("YES");
}   else printf("NO");

return 0;
}
TRViS
  • 23
  • 1
  • 7

1 Answers1

1

You don't initialize your variables so initially they contain random bogus values. That's not a problem for andrewGrapes, dmitryGrapes, michalGrapes, greenGrapes, purpleGrapes and blackGrapes because they will each be assigned a value by scanf (assuming each call succeeds).

However, andrewCheck will only be assigned a value when if (greenGrapes >= andrewGrapes) is true. dmitryCheck will only be assigned a value if if (greenGrapes + purpleGrapes >= dmitryGrapes) is true.

If any or both conditions are not true the respective variable will retain it's random bogus value and when you proceed to test those variables the result will be as such.

Unimportant
  • 2,076
  • 14
  • 19