题目
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 (<= 10^100).
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
解析
简单的字符串处理,掌握字符串流stringstream的基本用法可轻松解决,签到题。
代码
# include<bits/stdc++.h>
using namespace std;
string int2str(int x)
{
stringstream ss;
ss<<x;
return ss.str();
}
int main()
{
string s;
cin>>s;
int sum=0;
for(int i=0;i<s.size();++i)
{
sum+=s[i]-'0';
}
string ss=int2str(sum);
for(int i=0;i<ss.size();++i)
{
if(i)cout<<" ";
switch(ss[i])
{
case '0': cout<<"zero";break;
case '1': cout<<"one";break;
case '2': cout<<"two";break;
case '3': cout<<"three";break;
case '4': cout<<"four";break;
case '5': cout<<"five";break;
case '6': cout<<"six";break;
case '7': cout<<"seven";break;
case '8': cout<<"eight";break;
case '9': cout<<"nine";break;
default: break;
}
}
}