Средне Решено: 0 Процент решений: 0%

Number to Words

Spell a whole number in English words, such as "one hundred twenty-three".

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

  • Split a number into groups of three digits
  • Spell any number below 1000
  • Join word parts with exact spacing rules

Условие

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

Checks and invoices often show an amount in words. Write the number n in American English using these exact rules:

Implement number_to_words(n).

  • All letters are lowercase and words are separated by a single space. There is no "and" and there are no commas.
  • 0 to 19: zero, one, … nineteen. Tens: twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety.
  • A tens word and a units word are joined with a hyphen: 42 is forty-two.
  • Hundreds are written as <digit> hundred: 305 is three hundred five.
  • Groups of three digits get the scale word thousand, million or billion, and a group that is 0 is left out entirely: 1,000,005 is one million five.
  • zero is used only when n is 0.

Input

n: A non-negative integer.

Output

Return the words as one string.

Ограничения

0 ≤ n ≤ 999,999,999,999

Примеры

Пример 1
Ввод:
n = 123

Ожидаемый вывод:
expected = "one hundred twenty-three"
Ввод:
long long n = 123;

Ожидаемый вывод:
std::string expected = "one hundred twenty-three";
Ввод:
long n = 123;

Ожидаемый вывод:
string expected = "one hundred twenty-three";

Пояснение

One hundred, then twenty and three joined by a hyphen: "one hundred twenty-three".

Пример 2
Ввод:
n = 12045

Ожидаемый вывод:
expected = "twelve thousand forty-five"
Ввод:
long long n = 12045;

Ожидаемый вывод:
std::string expected = "twelve thousand forty-five";
Ввод:
long n = 12045;

Ожидаемый вывод:
string expected = "twelve thousand forty-five";

Пояснение

The thousands group is 12 and the rest is 45: "twelve thousand forty-five".

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

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

Подсказка 1

Write a helper that spells numbers from 1 to 999, then reuse it for each group.

Подсказка 2

Groups go from billions down to units; skip a group that is zero.

Подсказка 3

Keep separate word lists for 0 to 19 and for the tens.

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

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

Ссылки

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

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

Принятых решений пока нет. Решите задачу и займите первое место.

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

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

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.