knakul853

Untitled

Jun 19th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4.  
  5. class Solution {
  6.             string les_than_20 [20]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
  7.         string tens [20]= {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
  8.        
  9.         string thousands[5]={"", "Thousand", "Million", "Billion"};
  10. public:
  11.     string numberToWords(int num) {
  12.         if(num==0)return "Zero";
  13.         string ans="";
  14.         //x1x2x3x4x5x6x7
  15.         int i=0;
  16.            
  17.             while(num)
  18.             {
  19.                 if(num%1000!=0){
  20.                
  21.                   string temp="";
  22.                 f(temp, num % 1000);
  23.                 ans = temp + thousands[i] +" "+ ans;
  24.                 }
  25.                
  26.                 num/=1000;
  27.                 i++;
  28.             }
  29.             i=(int)ans.size()-1;
  30.         while(i >=0 && ans[i]==' ')ans.pop_back(), i--;
  31.        
  32.         return ans;
  33.      }
  34.    
  35.       public:
  36.  void f(string &temp, int n)
  37.    {
  38.      cout<<n<<"\n";
  39.       if(n==0)return;
  40.        if(n<20){
  41.            temp += les_than_20[ n ] + " ";
  42.            return;
  43.        }
  44.        if(n<100){
  45.            temp += tens[n/10]+" ";
  46.            f(temp, n %10);
  47.            return;
  48.        }
  49.      
  50.           temp += les_than_20[n/100]+" Hundred ";
  51.         f(temp, n%100);
  52.          return;
  53.      
  54.    }
  55.            
  56.  
  57. };
Add Comment
Please, Sign In to add comment