ปานกลาง 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก