1 / 8
Dec 2008

Well somehow i've run into a very trivial stupid situation in the problem "Elimination". The size of the input is not defined , so wat i do is grab each lines using getline() function , split every number into tokens (delimited by a space) and convert them back to integers.But this seems to be too slow and hence i'm gettin a TLE. Is there any other trick to get the input using scanf or something ? frowning

  • created

    Dec '08
  • last reply

    Dec '08
  • 7

    replies

  • 282

    views

  • 4

    users

  • 1

    link

Work with C strings; C++ strings are slow. Use gets(char*) to retrieve a line from stdin. To retrieve numbers you can iterate through the characters of the string one at a time (it would be an insult to your intelligence to detail exactly how to do that.)

yeah brian , wat u had said (abt c++ string being )slow is true (n m aware of it smile ). But wat i really wanna knw if there is any way i can read the input directly as ints n avoid any conversions (frm strings to int) in anyway ... thanks for the reply tho smile

If the input contains no spaces after last integer in each line then you can keep reading integers and break after you get a '\n'.

but do i check for a '\n' with only an int data type ? moreover scanf will skip over the whitespaces, so dat wud mean that it will skip over the '\n' too. even casting it to char wont work then.
or is it jus dat m missing something very basic confused

What I meant was something like this

while(1)
{
 scanf("%d",&n);
 if(getchar()=='\n')
  break;
}