I am getting a TLE. Please help.

I tried to code the first question of easy section and it is saying time limit exceeded? what shall i do? here is my code:

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

void main(void)
{

  int c;
  int x=0;
	
  
  
  while (scanf("\n%d", &c))
  {
		
		if(c == 42)       
		 {
		x=-1;
		}
		else
		{
			if(x== -1)
			{}
			else
			{
	printf("%d",c);
			}
		} 
  }

}
1 Like

Hello and welcome to Codechef. There are a few points which you must keep in mind while coding in Codechef (in C/C++):

  • The main function should always return a zero. Otherwise the judge will return a Runtime error verdict. So your main function should be like this:

    int main() 
    {
        //Your code
        return 0;
    }

  • To take input till the end do the following:
	
    while(scanf("\n%d", &c) != EOF) 
    {
        //Your code
    }

EOF (Short for End Of File) is returned by scanf function when there is no more data in the file to be input.

  • Print the output one line at a time:

    printf("%d\n", c);

  • Putting it all together:

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

int main(void)
{
    int c;
    int x=0;

    while (scanf("\n%d", &c) != EOF)
    {

        if(c == 42)       
        {
            x=-1;
        }
        else
        {
            if(x== -1)
            {}
            else
            {
                printf("%d\n",c);
            }
        } 
    }
    return 0;
}

3 Likes

I AGREE april 2017 Printable Calendar
Ram kumar Raj

may 2017 Calendar
may 2017 Calendar Printable YOUR ANSWER

Well that works but it is better to have a break statement after c==42 is tested true. If the input were designed to contain too many numbers after the first occurrence of 42 then it can be easily TLEd. Probably Codechef doesn’t want to scare away newbies :slight_smile:

1 Like

@gultus I agree. But I wanted to make minimal changes to the original code.