代码拉取完成,页面将自动刷新
class Solution
{
public:
vector<string> strList1 = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
vector<string> strList2 = {"Zero","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
vector<string> strList3 = {""," Thousand"," Million"," Billion"};
string translate(int num)
{
if(num == 0)
return "";
string result;
int hundred = num/100;
int decade = (num%100)/10;
int unit = num%10;
if(hundred != 0)
result += strList1[hundred] + " Hundred";
num %= 100;
if(num>0)
{
if(hundred != 0)
result += " ";
if(num<20)
result += strList1[num];
else
{
result += strList2[decade];
if(unit != 0)
result += " " + strList1[unit];
}
}
return result;
}
string numberToWords(int num)
{
if(num == 0)
return "Zero";
string result;
int temp;
int base = 1000000000;
for(int i = 3;i>=0;i--)
{
temp = (num/base)%1000;
if(temp !=0)
{
if(result.size() != 0)
result += " ";
result += translate(temp) + strList3[i];
}
base /= 1000;
}
return result;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。