Все задачи
Легко Решена: 1 Процент решений: 33.3%

Odd, Even, or Neither

Classify a number by whether it is an odd integer, an even integer, or neither.

Чему вы научитесь

  • Check even and odd with % 2
  • Detect values that are not integers
  • Order conditions correctly

Условие

Показать перевод

Whole numbers divisible by 2 are even; other whole numbers are odd. A number with a fractional part is neither. Zero is even, and negative integers follow the same rule.

Implement number_parity(number).

Input

number: The number to classify.

Output

Return odd, even, or neither as lowercase text.

Ограничения

−1,000,000 ≤ number ≤ 1,000,000. The input is finite and has at most two decimal places.

Примеры

Пример 1
Ввод:
number = 14

Ожидаемый вывод:
expected = "even"
Ввод:
double number = 14;

Ожидаемый вывод:
std::string expected = "even";
Ввод:
double number = 14;

Ожидаемый вывод:
string expected = "even";

Пояснение

14 = 2 × 7, so it is divisible by 2.

Пример 2
Ввод:
number = 3.5

Ожидаемый вывод:
expected = "neither"
Ввод:
double number = 3.5;

Ожидаемый вывод:
std::string expected = "neither";
Ввод:
double number = 3.5;

Ожидаемый вывод:
string expected = "neither";

Пояснение

3.5 has a fractional part, so it is neither an odd nor an even integer.

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

First decide whether the input is a whole number, since fractional values are neither odd nor even.

Подсказка 2

For whole numbers, parity depends on whether division by 2 leaves a remainder.

Подсказка 3

Remember that zero is even, and negative integers follow the same rule as positive ones.

Ожидаемая сложность

O(1) time, O(1) extra space

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

# Участник Язык Время Память Оценка ИИ Решение
1 GTGarlic The Destroyer Rust 1,634 ms - 4/5 Посмотреть код

Учитесь у других

Решения сообщества

Внимание, спойлер: ниже 1 полное решение Попробуйте сначала решить сами. Вы всегда можете посмотреть код других и AI-обзоры. Войдите и решите задание, чтобы присоединиться к обсуждению.