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

×

STRANGE problem in c

Buy1-Get1 like in many code chef problems ,this problem asks to input number of test case as first line.. so i wrote..

t=0; 
while((c=getchar())!='\n'&&c!=EOF) 
{ 
    t=t*10+c-'0'; 
}

id 1762981 this runs on ideone but gives WA in code chef so i wrote id 1762967

t=0;
scanf("%d\t",&t);

and now it got accepted... :) BUT WHY THIS HAPPENED?

asked 01 Feb '13, 16:53

vishfrnds's gravatar image

6★vishfrnds
47138
accept rate: 0%

edited 01 Feb '13, 17:07

betlista's gravatar image

3★betlista ♦♦
16.9k49115225

  • scanf("%dt",&t); //the '/' was missing in question typo..

(01 Feb '13, 16:56) vishfrnds6★

You are using c!=EOF it does not need here because you have already used c!='n' but better way you can use c>='0'...you can do following

 while((c=getc_(stdin))<'0'){}
    t=c;
    while(c>='0')
    {
    t=t*10+c-'0';
    c=getc(stdin);
    }
hope that it is better..

link

answered 03 Feb '13, 00:09

ajay154's gravatar image

3★ajay154
1.6k72044
accept rate: 8%

If you want to perform such low level programming, you should handle all special cases...

Does the problem statement specify the line ending?

You can try, if your read functions reads 7474 from following program

#include <cstdio>
int main() {
    printf( "7474\r\n" );
    return 0;
}

;-)

link

answered 01 Feb '13, 17:10

betlista's gravatar image

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

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:

×2,739
×1,491
×1,427
×340
×170

question asked: 01 Feb '13, 16:53

question was seen: 1,173 times

last updated: 03 Feb '13, 00:09