C gone mad

changing the method of input from “gets” to “scanf” corrects the program and reduces execution time from 50 sec to 1 sec!!!

http://www.codechef.com/viewsolution/1840667
http://www.codechef.com/viewsolution/1840657

why so?

scanf("%d",&test); 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",large), 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.

3 Likes

correct :slight_smile:

thax a lot sir :slight_smile:

//