If you know how to read numbers,you can make it.


  • 4
    N

    How to read large numbers

    Numbers are separated into groups: ones, tens, hundreds, thousands, millions, and so on. Each group contains three subgroups: ones, tens, and hundreds. When writing or reading a large number, begin at the left with the largest group, and proceed to the right. For instance, 7,482 is read as seven thousand, four hundred, eighty-two. The following chart can help in reading large numbers.

    enter image description here
    How to Read Large Numbers

       class Solution {
    public:
        string numberToWords(int num) {
            if(!num)return "Zero";
            string ones[20]={"Zero","One", "Two", "Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
            string tens[10]={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
            string m[5]={"Hundred", "Thousand", "Million","Billion","Trillion"};
            string s = "";
            int i=0;
            while(num){
            int t1000 = num%1000;
            string res="";
            if(t1000){
                    int t10=t1000%100;
                    if(t10){
                        if(t10<20)
                            res=ones[t10]+(res.empty()?"":" ")+res;
                        else {
                            int t1=t10%10;
                            if(t1){
                                res = ones[t1]+(res.empty()?"":" ")+res;
                            }
                            res=tens[t10/10]+(res.empty()?"":" ")+res;
                        }
                    }
                    int t100=t1000/100;
                    if(t100){
                        res=ones[t100]+" Hundred"+(res.empty()?"":" ")+res;
                    }
            if(i>0)
                res=res+" "+m[i];
            }
            i++;
            if(!res.empty())
                s=res+(s.empty()?"":" ")+s;
            num/=1000;
            }
            return s;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.