169人阅读 评论(0)

1005. Spell It Right (20)

400 ms

65536 kB

16000 B

Standard

CHEN, Yue

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:
12345

Sample Output:

one five

//1005. Spell It Right (20)
string ChangTostr(int n)//将数字对应到字符串
{
switch (n)
{
case 0:return "zero"; break;
case 1:return "one"; break;
case 2:return "two"; break;
case 3:return "three"; break;
case 4:return "four"; break;
case 5:return "five"; break;
case 6:return "six"; break;
case 7:return "seven"; break;
case 8:return "eight"; break;
case 9:return "nine"; break;
}
}

int main()
{
string s;
cin >> s;
int sum = 0;
for (int i = 0; i < s.length(); i++)
{
int num = s[i] - '0';
sum += num;//计算和
}
stringstream ss;
ss << sum;
string str = ss.str();//将整型转为字符串，方便理解
for (int i = 0; i < str.length(); i++)
{
int num = str[i] - '0';
if (i)
{
cout << " " << ChangTostr(num);
}
else
{
cout << ChangTostr(num);
}
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：53725次
• 积分：1402
• 等级：
• 排名：千里之外
• 原创：77篇
• 转载：84篇
• 译文：0篇
• 评论：4条
阅读排行
最新评论