LeetCode 273 Integer to English Words
递归实现billion million thousand hundred 的实现
可以通过num与1000000000, 1000000, 1000, 50, 1的比较来实现不同的invert
1 string num1[20] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}; 2 string num2[10] = {"Zero","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; 3 string invert(int num){ 4 string str = ""; 5 if(num >= 1000000000){ 6 return invert(num/1000000000)+" Billion" + invert(num%1000000000); 7 }else if(num >= 1000000){ 8 return invert(num/1000000)+ " Million" + invert(num%1000000); 9 }else if(num >= 1000){ 10 return invert(num/1000) + " Thousand" + invert(num%1000); 11 }else if(num >= 100){ 12 return invert(num/100) + " Hundred" + invert(num%100); 13 }else if(num >= 20){ 14 return " " + num2[num/10] + invert(num%10); 15 }else if(num >=1){ 16 return " " + num1[num]; 17 }else{ 18 return ""; 19 } 20 } 21 //程序入口 22 string numberToWords(int num) { 23 if(num==0) return num1[num]; 24 else{ 25 string result = invert(num); 26 result.erase(result.begin()); 27 return result; 28 } 29 }