题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
#include <iostream>
using namespace std;
string ones[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string tens[] = { "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen" };
string twenties[] = { "zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
string hundreds[] = { "hundred", "thousand", "million", "billion" };
string translate(int y,int x,int pos)
{
string strOut;
int hundred = y/100;
int left = y%100;
if(hundred > 0)
{
strOut = ones[hundred] + " " + hundreds[0];
}
if(left > 0)
{
if(!strOut.empty())
strOut += " and ";
if(left >= 10 &&left < 20)
{
strOut += tens[left-10];
}
else if(left < 10)
{
strOut += ones[left];
}
else
{
int ten = left/10;
int one = left%10;
strOut += twenties[ten];
if(one > 0)
{
strOut += " " + ones[one];
}
}
}
if(x >0)
{
if(!strOut.empty())
{
strOut = hundreds[pos+1] + " " + strOut;
}
else
{
strOut = hundreds[pos+1];
}
}
return strOut;
};
int main() {
long a;
while (cin >> a) { // 注意 while 处理多个 case
string strOut;
int pos = 0;
int x = a, y = a%1000;
do
{
x = x/1000;
string out = translate(y,x,pos);
if(!out.empty())
{
if(strOut.empty())
{
strOut = out;
}
else
{
strOut = out + " " + strOut;
}
}
++pos;
y = x%1000;
}while (x > 0);
cout<<strOut<<endl;
}
}
// 64 位输出请用 printf("%lld")