Programa, skirta konvertuoti nurodytą numerį į žodžius | 2 rinkinys

Programa, skirta konvertuoti nurodytą numerį į žodžius | 2 rinkinys
Išbandykite tai GFG praktikoje #practicelinkdiv {ekranas: nėra! SVARBU; }

Parašykite kodą, kad nurodytą numerį konvertuotumėte žodžiais.

Pavyzdžiai:  

Įvestis: 438237764
Išvestis: Keturiasdešimt trys crore aštuoniasdešimt du lakai trisdešimt septyni tūkstančiai septyni šimtai šeši šešiasdešimt keturi

Įvestis: 999999
Išvestis: Devyni lakai devyniasdešimt devyni tūkstančiai devyni šimtai devyniasdešimt devyni

Įvestis: 1000
Išvestis: Tūkstantis
Paaiškinimas: 1000 žodžių yra „vienas tūkstantis“

Rekomenduojama praktika Sveikasis žodžių skaičius Išbandykite!

Mes jau aptarėme požiūrį, kuris tvarko skaičius nuo 0 iki 9999 ankstesnis Skelbimas.

Sprendimas: Šis požiūris gali tvarkyti skaičių iki 20 skaitmenų ilgio, kuris yra mažesnis nei Ullong_max (Maksimali tipo objekto neparašyto ilgo INT objekto vertė). Ullong_max yra lygus 18446744073709551615 dešimtainiu, darant prielaidą, kad kompiliatorius užtrunka 8 baitus, kad būtų laikomas nepasirašytas ilgas ilgas int.

Žemiau pateiktas vaizdas rodo bet kurio 9 skaitmenų teigiamo skaičių vertės diagramą: Teigiamas sveikasis skaičius: 

 4 3 8 2 3 7 7 6 4   
| | | | | | | | |__ ones' place
| | | | | | | |__ __ tens' place
| | | | | | |__ __ __ hundreds' place
| | | | | |__ __ __ __ thousands' place
| | | | |__ __ __ __ __ tens thousands' place
| | | |__ __ __ __ __ __ hundred thousands' place
| | |__ __ __ __ __ __ __ one millions' place
| |__ __ __ __ __ __ __ __ ten millions' place
|__ __ __ __ __ __ __ __ __ hundred millions' place

Idėja yra suskirstyti skaičių į atskirus skaitmenis pagal aukščiau pateiktą vietos vertės diagramą ir tvarkyti jas, pradedant nuo reikšmingiausio skaitmens.

Štai paprastas įgyvendinimas, palaikantis skaičius, kurių skaičius yra ne daugiau kaip 9. Programą galima lengvai išplėsti, kad būtų galima palaikyti bet kokį 20 skaitmenų numerį.

C++
   /* C++ program to print a given number in words.    The program handles till 9 digits numbers and    can be easily extended to 20 digit number */   #include          using     namespace     std  ;   // strings at index 0 is not used it is to make array   // indexing simple   string     one  []     =     {     ''       'one '       'two '       'three '       'four '        'five '       'six '       'seven '       'eight '        'nine '       'ten '       'eleven '       'twelve '        'thirteen '       'fourteen '       'fifteen '        'sixteen '       'seventeen '       'eighteen '        'nineteen '     };   // strings at index 0 and 1 are not used they are to   // make array indexing simple   string     ten  []     =     {     ''       ''       'twenty '       'thirty '       'forty '        'fifty '       'sixty '       'seventy '       'eighty '        'ninety '     };   // n is 1- or 2-digit number   string     numToWords  (  int     n       string     s  )   {      string     str     =     ''  ;      // if n is more than 19 divide it      if     (  n     >     19  )      str     +=     ten  [  n     /     10  ]     +     one  [  n     %     10  ];      else      str     +=     one  [  n  ];      // if n is non-zero      if     (  n  )      str     +=     s  ;      return     str  ;   }   // Function to print a given number in words   string     convertToWords  (  long     n  )   {      // stores word representation of given number n      string     out  ;      // handles digits at ten millions and hundred      // millions places (if any)      out     +=     numToWords  ((  n     /     10000000  )     'crore '  );      // handles digits at hundred thousands and one      // millions places (if any)      out     +=     numToWords  (((  n     /     100000  )     %     100  )     'lakh '  );      // handles digits at thousands and tens thousands      // places (if any)      out     +=     numToWords  (((  n     /     1000  )     %     100  )     'thousand '  );      // handles digit at hundreds places (if any)      out     +=     numToWords  (((  n     /     100  )     %     10  )     'hundred '  );      if     (  n     >     100     &&     n     %     100  )      out     +=     'and '  ;      // handles digits at ones and tens places (if any)      out     +=     numToWords  ((  n     %     100  )     ''  );          //Handling the n=0 case      if  (  out  ==  ''  )      out     =     'zero'  ;      return     out  ;   }   // Driver code   int     main  ()   {      // long handles upto 9 digit no      // change to unsigned long long int to      // handle more digit number      long     n     =     438237764  ;      // convert given number in words      cout      < <     convertToWords  (  n  )      < <     endl  ;      return     0  ;   }   
Java
   /* Java program to print a given number in words.    The program handles till 9 digits numbers and    can be easily extended to 20 digit number */   class   GFG     {      // Strings at index 0 is not used it is to make array      // indexing simple      static     String     one  []     =     {     ''       'one '       'two '       'three '       'four '        'five '       'six '       'seven '       'eight '        'nine '       'ten '       'eleven '       'twelve '        'thirteen '       'fourteen '       'fifteen '        'sixteen '       'seventeen '       'eighteen '        'nineteen '     };      // Strings at index 0 and 1 are not used they are to      // make array indexing simple      static     String     ten  []     =     {     ''       ''       'twenty '       'thirty '       'forty '        'fifty '       'sixty '       'seventy '       'eighty '        'ninety '     };      // n is 1- or 2-digit number      static     String     numToWords  (  int     n       String     s  )      {      String     str     =     ''  ;      // if n is more than 19 divide it      if     (  n     >     19  )     {      str     +=     ten  [  n     /     10  ]     +     one  [  n     %     10  ]  ;      }      else     {      str     +=     one  [  n  ]  ;      }      // if n is non-zero      if     (  n     !=     0  )     {      str     +=     s  ;      }      return     str  ;      }      // Function to print a given number in words      static     String     convertToWords  (  long     n  )      {      // stores word representation of given number n      String     out     =     ''  ;      // handles digits at ten millions and hundred      // millions places (if any)      out     +=     numToWords  ((  int  )(  n     /     10000000  )     'crore '  );      // handles digits at hundred thousands and one      // millions places (if any)      out     +=     numToWords  ((  int  )((  n     /     100000  )     %     100  )     'lakh '  );      // handles digits at thousands and tens thousands      // places (if any)      out     +=     numToWords  ((  int  )((  n     /     1000  )     %     100  )     'thousand '  );      // handles digit at hundreds places (if any)      out     +=     numToWords  ((  int  )((  n     /     100  )     %     10  )     'hundred '  );      if     (  n     >     100     &&     n     %     100     >     0  )     {      out     +=     'and '  ;      }      // handles digits at ones and tens places (if any)      out     +=     numToWords  ((  int  )(  n     %     100  )     ''  );      return     out  ;      }      // Driver code      public     static     void     main  (  String  []     args  )      {      // long handles upto 9 digit no      // change to unsigned long long int to      // handle more digit number      long     n     =     438237764  ;      // convert given number in words      System  .  out  .  printf  (  convertToWords  (  n  ));      }   }   
Python3
   # Python3 program to print a given number in words.   # The program handles till 9 digits numbers and   # can be easily extended to 20 digit number    # strings at index 0 is not used it    # is to make array indexing simple   one   =   [   ''     'one '     'two '     'three '     'four '     'five '     'six '     'seven '     'eight '     'nine '     'ten '     'eleven '     'twelve '     'thirteen '     'fourteen '     'fifteen '     'sixteen '     'seventeen '     'eighteen '     'nineteen '  ];   # strings at index 0 and 1 are not used    # they are to make array indexing simple   ten   =   [   ''     ''     'twenty '     'thirty '     'forty '     'fifty '     'sixty '     'seventy '     'eighty '     'ninety '  ];   # n is 1- or 2-digit number   def   numToWords  (  n     s  ):   str   =   ''  ;   # if n is more than 19 divide it   if   (  n   >   19  ):   str   +=   ten  [  n   //   10  ]   +   one  [  n   %   10  ];   else  :   str   +=   one  [  n  ];   # if n is non-zero   if   (  n  ):   str   +=   s  ;   return   str  ;   # Function to print a given number in words   def   convertToWords  (  n  ):   # stores word representation of given    # number n   out   =   ''  ;   # handles digits at ten millions and    # hundred millions places (if any)   out   +=   numToWords  ((  n   //   10000000  )   'crore '  );   # handles digits at hundred thousands    # and one millions places (if any)   out   +=   numToWords  (((  n   //   100000  )   %   100  )   'lakh '  );   # handles digits at thousands and tens    # thousands places (if any)   out   +=   numToWords  (((  n   //   1000  )   %   100  )   'thousand '  );   # handles digit at hundreds places (if any)   out   +=   numToWords  (((  n   //   100  )   %   10  )   'hundred '  );   if   (  n   >   100   and   n   %   100  ):   out   +=   'and '  ;   # handles digits at ones and tens   # places (if any)   out   +=   numToWords  ((  n   %   100  )   ''  );   return   out  ;   # Driver code   # long handles upto 9 digit no   # change to unsigned long long    # int to handle more digit number   n   =   438237764  ;   # convert given number in words   print  (  convertToWords  (  n  ));   # This code is contributed by mits   
C#
   /* C# program to print a given number in words.    The program handles till 9 digits numbers and    can be easily extended to 20 digit number */   using     System  ;   class     GFG     {      // strings at index 0 is not used it is      // to make array indexing simple      static     string  []     one     =     {     ''       'one '       'two '       'three '       'four '        'five '       'six '       'seven '       'eight '        'nine '       'ten '       'eleven '       'twelve '        'thirteen '       'fourteen '       'fifteen '        'sixteen '       'seventeen '       'eighteen '        'nineteen '     };      // strings at index 0 and 1 are not used      // they are to make array indexing simple      static     string  []     ten     =     {     ''       ''       'twenty '       'thirty '       'forty '        'fifty '       'sixty '       'seventy '       'eighty '        'ninety '     };      // n is 1- or 2-digit number      static     string     numToWords  (  int     n       string     s  )      {      string     str     =     ''  ;      // if n is more than 19 divide it      if     (  n     >     19  )     {      str     +=     ten  [  n     /     10  ]     +     one  [  n     %     10  ];      }      else     {      str     +=     one  [  n  ];      }      // if n is non-zero      if     (  n     !=     0  )     {      str     +=     s  ;      }      return     str  ;      }      // Function to print a given number in words      static     string     convertToWords  (  long     n  )      {      // stores word representation of      // given number n      string     out1     =     ''  ;      // handles digits at ten millions and      // hundred millions places (if any)      out1     +=     numToWords  ((  int  )(  n     /     10000000  )      'crore '  );      // handles digits at hundred thousands      // and one millions places (if any)      out1     +=     numToWords  ((  int  )((  n     /     100000  )     %     100  )      'lakh '  );      // handles digits at thousands and tens      // thousands places (if any)      out1     +=     numToWords  ((  int  )((  n     /     1000  )     %     100  )      'thousand '  );      // handles digit at hundreds places (if any)      out1     +=     numToWords  ((  int  )((  n     /     100  )     %     10  )      'hundred '  );      if     (  n     >     100     &&     n     %     100     >     0  )     {      out1     +=     'and '  ;      }      // handles digits at ones and tens      // places (if any)      out1     +=     numToWords  ((  int  )(  n     %     100  )     ''  );      return     out1  ;      }      // Driver code      static     void     Main  ()      {      // long handles upto 9 digit no      // change to unsigned long long int to      // handle more digit number      long     n     =     438237764  ;      // convert given number in words      Console  .  WriteLine  (  convertToWords  (  n  ));      }   }   // This code is contributed by mits   
JavaScript
    <  script  >   /* Javascript program to     print a given number in words.     The program handles till 9    digits numbers and    can be easily extended to   20 digit number */      // Strings at index 0 is not used it is to make array      // indexing simple      var     one     =     [     ''       'one '       'two '       'three '       'four '        'five '       'six '       'seven '       'eight '        'nine '       'ten '       'eleven '       'twelve '        'thirteen '       'fourteen '       'fifteen '        'sixteen '       'seventeen '       'eighteen '        'nineteen '     ];      // Strings at index 0 and 1 are not used they are to      // make array indexing simple      var     ten     =     [     ''       ''       'twenty '       'thirty '       'forty '        'fifty '       'sixty '       'seventy '       'eighty '        'ninety '     ];      // n is 1- or 2-digit number      function     numToWords  (  n       s  )      {      var     str     =     ''  ;      // if n is more than 19 divide it      if     (  n     >     19  )     {      str     +=     ten  [  parseInt  (  n     /     10  )]     +     one  [  n     %     10  ];      }      else     {      str     +=     one  [  n  ];      }      // if n is non-zero      if     (  n     !=     0  )     {      str     +=     s  ;      }      return     str  ;      }      // Function to print a given number in words      function     convertToWords  (  n  )      {      // stores word representation of given number n      var     out     =     ''  ;      // handles digits at ten millions and hundred      // millions places (if any)      out     +=     numToWords  (  parseInt  (  n     /     10000000  )         'crore '  );      // handles digits at hundred thousands and one      // millions places (if any)      out     +=     numToWords  (  parseInt  ((  n     /     100000  )     %     100  )      'lakh '  );      // handles digits at thousands and tens thousands      // places (if any)      out     +=     numToWords  (  parseInt  ((  n     /     1000  )     %     100  )         'thousand '  );      // handles digit at hundreds places (if any)      out     +=     numToWords  (  parseInt  ((  n     /     100  )     %     10  )         'hundred '  );      if     (  n     >     100     &&     n     %     100     >     0  )     {      out     +=     'and '  ;      }      // handles digits at ones and tens places (if any)      out     +=     numToWords  (  parseInt  (  n     %     100  )     ''  );      return     out  ;      }      // Driver code      // var handles upto 9 digit no      // change to unsigned var var var to      // handle more digit number      var     n     =     438237764  ;      // convert given number in words      document  .  write  (  convertToWords  (  n  ));       // This code is contributed by Amit Katiyar     <  /script>   
PHP
      /* PHP program to print a given number in words.   The program handles till 9 digits numbers and   can be easily extended to 20 digit number */   // strings at index 0 is not used it is    // to make array indexing simple   $one   =   array  (  ''     'one '     'two '     'three '     'four '     'five '     'six '     'seven '     'eight '     'nine '     'ten '     'eleven '     'twelve '     'thirteen '     'fourteen '     'fifteen '     'sixteen '     'seventeen '     'eighteen '     'nineteen '  );   // strings at index 0 and 1 are not used    // they are to make array indexing simple   $ten   =   array  (  ''     ''     'twenty '     'thirty '     'forty '     'fifty '     'sixty '     'seventy '     'eighty '     'ninety '  );   // n is 1- or 2-digit number   function   numToWords  (  $n     $s  )   {   global   $one     $ten  ;   $str   =   ''  ;   // if n is more than 19 divide it   if   (  $n   >   19  )   {   $str   .=   $ten  [(  int  )(  $n   /   10  )];   $str   .=   $one  [  $n   %   10  ];   }   else   $str   .=   $one  [  $n  ];   // if n is non-zero   if   (  $n   !=   0   )   $str   .=   $s  ;   return   $str  ;   }   // Function to print a given number in words   function   convertToWords  (  $n  )   {   // stores word representation of    // given number n   $out   =   ''  ;   // handles digits at ten millions and    // hundred millions places (if any)   $out   .=   numToWords  ((  int  )(  $n   /   10000000  )   'crore '  );   // handles digits at hundred thousands    // and one millions places (if any)   $out   .=   numToWords  (((  int  )(  $n   /   100000  )   %   100  )   'lakh '  );   // handles digits at thousands and tens   // thousands places (if any)   $out   .=   numToWords  (((  int  )(  $n   /   1000  )   %   100  )   'thousand '  );   // handles digit at hundreds places (if any)   $out   .=   numToWords  (((  int  )(  $n   /   100  )   %   10  )   'hundred '  );   if   (  $n   >   100   &&   $n   %   100  )   $out   .=   'and '  ;   // handles digits at ones and tens   // places (if any)   $out   .=   numToWords  ((  $n   %   100  )   ''  );   return   $out  ;   }   // Driver code   // long handles upto 9 digit no   // change to unsigned long long int to   // handle more digit number   $n   =   438237764  ;   // convert given number in words   echo   convertToWords  (  $n  )   .   '  n  '  ;   // This code is contributed by Akanksha Rai   ?>   

Išvestis
forty three crore eighty two lakh thirty seven thousand seven hundred and sixty four  

Sudėtingumo analizė:  

  • Laiko sudėtingumas: O (1). 
    Kilpa veikia pastovų laiką.
  • Pagalbinė erdvė: O (1). 
    Nes nereikia jokios papildomos vietos.