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

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

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

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

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

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