题目描述
有一个非负整数,请编写一个算法,打印该整数的英文描述。
给定一个int x,请返回一个string,为该整数的英文描述。
测试样例:
1234
返回:”One Thousand,Two Hundred Thirty Four”
class ToString {
public:
vector<string> digits = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten" };
vector<string> teens = {"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
vector<string> tens = {"","","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
vector<string> bigs = { "Hundred", "Thousand", "Million","Billion"};
string fayin(int x)
{
string res;
if(x < 0)
return res;
if(x <= 10 && x >0)
res = digits[x];
else if(x < 20&& x > 10)
res = teens[x-10];
else if(x < 100&& x >=20)
res = tens[x/10]+ (x % 10 > 0 ? " " : "") +fayin(x%10);
else if(x < 1000 && x >=100)
res = fayin(x/100) + " " +bigs[0] + (x % 100 > 0 ? " " : "") +fayin(x%100);
else if(x < 1000000 && x >=1000)
res = fayin(x/1000) + " " +bigs[1] + (x % 1000 > 0 ? "," : "") + fayin(x%1000);
else if(x < 1000000000 && x >= 1000000)
res = fayin(x/1000000) + " " +bigs[2] + (x % 1000000 > 0 ? "," : "") + fayin(x%1000000);
else if(x < 1000000000000 && x >= 1000000000)
res = fayin(x/1000000000) + " " +bigs[3] + (x % 1000000000 > 0 ? "," : "") + fayin(x%1000000000);
return res;
}
string toString(int x) {
// write code here
string res;
if(x < 0)
return res;
else if(x == 0)
res = "zero";
else
res = fayin(x);
return res;
}
};