ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1644. Куча орехов

How to read input?
Послано 2rf [Perm School #9] 7 авг 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?
Послано Sergey Lazarev (MSU Tashkent) 7 авг 2009 18:21
char s[20];
for (int i=0; i<n; i++){
   scanf("%d %s",&x,s);
   ...
}
Re: How to read input?
Послано 2rf [Perm School #9] 7 авг 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?
Послано Sergey Lazarev (MSU Tashkent) 7 авг 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