中級 0件解決済み 正答率0%

Number Letter Counts

Count the letters needed to write every number from 1 to n in English words.

このチャレンジで学べること

  • Spell numbers in English words
  • Split a number into thousands, hundreds, tens and ones
  • Count letters while ignoring spaces and hyphens

問題

翻訳を表示

A sign maker spells out house numbers in words and pays by the letter. Write every number from 1 to n in American English, without the word "and": 342 is "three hundred forty-two" and 2005 is "two thousand five". Spaces and hyphens are free; only letters count.

Implement number_letter_counts(n).

Spellings: one to nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand. Numbers from 1,000 up are written as the thousands part (itself in words, up to ninety-nine) followed by "thousand" and then the rest, if any.

Input

n: The last number to write.

Output

Return the total number of letters used for 1, 2, ..., n. For n = 0 nothing is written and the answer is 0.

制約

0 ≤ n ≤ 99,999

例

例 1
入力:
n = 3

期待される出力:
expected = 11
入力:
long long n = 3;

期待される出力:
long long expected = 11;
入力:
long n = 3;

期待される出力:
long expected = 11;

解説

"one", "two" and "three" have 3 + 3 + 5 = 11 letters.

例 2
入力:
n = 21

期待される出力:
expected = 121
入力:
long long n = 21;

期待される出力:
long long expected = 121;
入力:
long n = 21;

期待される出力:
long expected = 121;

解説

1 to 19 use 106 letters, twenty adds 6 and twenty-one adds 9, for 121.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Handle 1 to 19 with a table, then the tens words for 20 to 90.

ヒント 2

A three-digit block is the hundreds word plus "hundred" plus the last two digits.

ヒント 3

For numbers of 1,000 and up, spell the thousands block, add "thousand", then spell the rest.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。