You are not logged in. Please login at www.codechef.com to post your questions!

×

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);
            }
        } 
  }

}

asked 15 Mar '12, 17:31

bhawnaj's gravatar image

2★bhawnaj
16223
accept rate: 0%

edited 16 Mar '12, 01:29

admin's gravatar image

0★admin ♦♦
19.8k350498541


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;
}
link

answered 15 Mar '12, 17:58

balajiganapath's gravatar image

6★balajiganapath ♦♦
75542742
accept rate: 77%

edited 15 Mar '12, 18:45

1

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 :)

(15 Mar '12, 19:50) gultus ♦4★

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

(16 Mar '12, 09:30) balajiganapath ♦♦6★
link

answered 06 Feb '17, 12:30

anujpandit's gravatar image

0★anujpandit
-83
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×720

question asked: 15 Mar '12, 17:31

question was seen: 1,780 times

last updated: 06 Feb '17, 12:30