मध्यम 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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।