why wrong answer?

Take a look at my C program…
it gives wrong ans at codechef…


The problem seems to be because of the gets() getting the left \n of the previous scanf statement. Similar problems was faced here http://discuss.codechef.com/questions/6509/c-gone-mad.

scanf("%d",&b); actually takes the first next in the input and leaves the \n following in there only, so now the input pointer is at \n. gets() function reads from input till the next newline character, therefore getting an empty string, making the program wrong, while if there was scanf("%s",a), it would neglect the first newline, read the next integer and hence program works.

Therefore, whenever you’re using a mix of scanf and gets, do a dummy gets (or a getchar) after the scanf to resolve errors like this.

Also, you are not doing m++ in the first case!

1 Like

http://www.codechef.com/viewsolution/1954370 here is the link to your corrected code. The reason for wrong answer is already described by @piyushkumar

@ piyushkumar
@ amitupadhyay


your welcome

Take a look at my C program… it gives wrong ans at codechef…

The num variable in your program becomes greater than 1e9+7 in the while loop and since num is int type it gets negative after INT_MAX, 2147483647 and show wrong ans.
Take modulo of the num variable every time the loop iterates in the inner while loop @bright_future