其他分享
首页 > 其他分享> > 273. 整数转换英文表示

273. 整数转换英文表示

作者:互联网

 1 //用到了贪心思想
 2 class Solution 
 3 {
 4 public:
 5     string small[20] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
 6                         "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
 7     string decade[10] = {"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
 8     string big[4] = {"Billion","Million","Thousand"," "};
 9 public:
10     string numberToWords(int num) 
11     {
12         if(!num) return small[0];
13 
14         string res;
15         for(int i = 1000000000,j = 0;i > 0;i /= 1000,j ++)
16         {
17             if(num >= i)
18             {
19                 res += get_part(num / i) + big[j] + ' ';
20                 num %= i;
21             }
22         }
23         while(res.back() == ' ') res.pop_back();
24         return res;
25     }
26 
27     string get_part(int num)//0-999的数
28     {
29         string res;
30         if(num >= 100)
31         {
32             res += small[num / 100] + " Hundred ";
33             num %= 100;
34         }
35         if(!num) return res;
36         if(num >= 20) 
37         {
38             res += decade[num / 10] + ' ';
39             num %= 10;
40         }
41         if(!num) return res;
42         res += small[num] + ' ';
43         return res;
44     }
45 };

 此题很难

标签:10,num,string,res,整数,273,英文,small,return
来源: https://www.cnblogs.com/yuhong1103/p/12561885.html