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

×

running problem in code

i made a code.but while compiling time limit exceeded is displayed.so how can i run this program.

asked 23 May '12, 20:49

prachi94's gravatar image

2★prachi94
61234
accept rate: 0%

retagged 23 May '12, 20:57

betlista's gravatar image

3★betlista ♦♦
16.9k49115225


@prachi94 I think you are talking about your following code:

#include<stdio.h>
main()
{
    int i;
    scanf("%d",&i);
    while(i!=42 || i<0)
    {
        printf("%d",i);
    }
}

Let me highlight the problem statement:

You have to print the input and Stop processing/printing input after reading in the number 42.*

But does your solution really follow the problem statement ?

No.

Just run the while loop ,keep taking input ,check if the input is 42.If yes,break the loop ,otherwise just print the input.Well ,that's all you have to do.

Your solution only read the input once , if the input is not 42 ,then the WHILE loop run for ever(ie. it becomes infinite loop).Your program does not terminate and that's the reason you got a time limit.

In order to resolve it just add one extra line scanf("%d",&i); after printf("%d",i); statement:

#include<stdio.h>
main()
{
    int i;
    scanf("%d",&i);
    while(i!=42 || i<0)
    {

        printf("%d\n",i);//Also u were missing the newline character  here
                                     //Your soln output format and problem output format 
                                     //should match exactly
        scanf("%d",&i); //Keep taking input,if the data you read is not 42
    }
     return 0; //Don't forget return 0 ,You will get runtym error if u miss it.
}
link

answered 23 May '12, 21:43

ritesh_gupta's gravatar image

5★ritesh_gupta ♦
3.7k42549
accept rate: 27%

edited 23 May '12, 21:59

You can (and you should) test your code. I'm doing it like this: I copy statement input to file (for example named test.in) and then I run

test.exe < test.in

or

test.exe < test.in > test.out

And then I compare my output with the one in statement...

You are getting TLE, you can read about the errors here.

link

answered 23 May '12, 20:56

betlista's gravatar image

3★betlista ♦♦
16.9k49115225
accept rate: 11%

Answer is hidden as author is suspended. Click here to view.

answered 26 May '12, 02:42

marcswing's gravatar image

0★marcswing
(suspended)
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:

×729
×690
×198

question asked: 23 May '12, 20:49

question was seen: 1,232 times

last updated: 26 May '12, 02:42