Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Number Letter Counts

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ n ≤ 99,999

Ví dụ

Ví dụ 1
Đầu vào:
n = 3

Kết quả mong đợi:
expected = 11
Đầu vào:
long long n = 3;

Kết quả mong đợi:
long long expected = 11;
Đầu vào:
long n = 3;

Kết quả mong đợi:
long expected = 11;

Giải thích

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

Ví dụ 2
Đầu vào:
n = 21

Kết quả mong đợi:
expected = 121
Đầu vào:
long long n = 21;

Kết quả mong đợi:
long long expected = 121;
Đầu vào:
long n = 21;

Kết quả mong đợi:
long expected = 121;

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.