stone game one solution works and the other doesn't, why?

Hi, I solved the Stone Game problem with this solution:

I’m trying to solve it getting input with gets.

The sample input works well, but the solution doesn’t pass codechef test.

This is one of the solutions that don’t work:

Could you help me understanding why?

Thank you.

1 Like

use that code for input generation and than run your not working code

#include <cstdio>

int main() {
	printf( "%d\r\n", 2);
	printf( "%d\r\n", 2);
	printf( "%d %d\r\n", 1, 2);
	printf( "%d\r\n", 3);
	printf( "%d %d %d\r\n", 1, 2, 3);
	return 0;

it returns “BOB”, “BOB”, but it has to be “BOB”, “ALICE”, quite hard to find…

1 Like

@beautybrain why did you vote down my answer? is it incorrect?

It gives me a segmentation fault, because you first say “1 test case, 100 piles” but then you enter 1000 stones FOR 1000 piles (the for condition is wrong, it should be ‘i<100’ instead of ‘i<1000’), right?

After I’ve corrected that, the answer is the same as the other solution. 1 says bob, and the “wrong” solution says bob too. I’m still stuck at understanding why my other solution is wrong :frowning: I made the buffer 500 elements big, because in the “biggest” scenario… I would need 5 char 100 times, like '1000 '. But, when the last ‘1000’ comes, it would have a ‘\0’ instead of a ’ ’ after it.

I’m sorry about the incorrect code before, it’s updated, try now :wink:

So the problem was in the additional escape sequence ‘\r’ ?
The getchar()s only ate the ‘\r’, leaving the ‘\n’ in the input buffer.
So the gets() would only get a ‘\n’ and continue to the next test case.
Was this the problem? :stuck_out_tongue:

I fixed it with a double getchar() after every Enter (I suppose I could also use a loop like while((ch = getchar()) != EOF && ch != ‘\n’); right before the gets() call).

Thank you!