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

Обсуждение задачи 1767. Дом доктора Ди

Why doesn't labs() work?
Послано Avatar 12 апр 2010 11:04
I got WA6 in this case:
if (inc1 == inc2) {
    cout << labs(cr2x - cr1x) + labs(cr2y - cr1y);
    return;
}
cout << max(labs(cr2x - cr1x), labs(cr2y - cr1y));

but I got AC in this case:
if (inc1 == inc2) {
    cout << (cr2x - cr1x) + labs(cr2y - cr1y);
    return;
}
cout << max(labs(cr2x - cr1x), labs(cr2y - cr1y));

and in this case:
if (inc1 == inc2) {
    cout << labs(cr2x - cr1x) + (cr2y - cr1y);
    return;
}
cout << max(labs(cr2x - cr1x), labs(cr2y - cr1y));

and in this:
if (inc1 == inc2) {
    cout << (cr2x - cr1x) + (cr2y - cr1y);
    return;
}
cout << max(labs(cr2x - cr1x), labs(cr2y - cr1y));

p.s. cr1x, cr1y, cr2x, cr2y is long long, (cr2x - cr1x)>=0 and (cr2y - cr1y)>=0 always, but why in this case does not labs() work correctly ?
Re: Why doesn't labs() work?
Послано Alipov Vyacheslav [Tomsk PU] 12 апр 2010 22:03
  Letter 'l' in "labs" stands for "long" = "int" (not "long long").
  So in the first case two summands are converted from "long long" to "int" and the summation causes overflow.
  In the second and the third cases one of the summands has type "long long" so the sum is in type "long long" too.

Edited by author 12.04.2010 22:11