ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1644. A Whole Lot of Walnuts

How to read input?
Posted by 2rf [Perm School #9] 7 Aug 2009 18:01
I got AC at this easy problem but I'm just interested: how did you read input? I used this:

    for (int i=0; i<n; i++) {
        scanf("%d %c",&x,&ch);
        getline(cin,tmp);
//something...
    }

But I think it's stupid way of doing this, can someone with C++ AC show me his reading part?

Edited by author 07.08.2009 18:01
Re: How to read input?
Posted by Sergey Lazarev (MSU Tashkent) 7 Aug 2009 18:21
char s[20];
for (int i=0; i<n; i++){
   scanf("%d %s",&x,s);
   ...
}
Re: How to read input?
Posted by 2rf [Perm School #9] 7 Aug 2009 19:16
well, this is good but i wanted something else...

Is there anything in C++ which works as ReadLn in Pascal. I mean something which read and ignore characters before next newline character? I thought something like scanf("%d %c\n",&x,&ch) works but it doesn't. getline works fine but it requires to declare one more string variable and works slow.

Also, can you explain me how scanf("%s",s) works?
Re: How to read input?
Posted by Sergey Lazarev (MSU Tashkent) 7 Aug 2009 21:28
cin.ignore(maxCharsNum,delimiterChar);

It ignores "maxCharsNum" symbols if there is no "delimiterChar" among them. Else it ignores all symbols before "delimiterChar" (and "delimiterChar" also).


char s[N];
scanf("%s",s);

Reads symbols before meet space, '\n' or '\t' and puts '\0' in the end. It doesn't stop reading if number of symbols is greater than N. So, N must be always greater than maximal number of symbols in string.

Edited by author 07.08.2009 21:28