1005 Spell It Right (20分)
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
这道题首先输入时应该用string输入,否则可能会数据溢出,string输入后依次相加后,再转化为string,通过数组映射输出就完成了,比较简单,也没什么难点
#include<bits/stdc++.h>
using namespace std;
string d[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int main()
{
string a;
cin>>a;
int sum=0;
for(int i=0;i<a.length();i++){
sum+=a[i]-'0';
}
string c=to_string(sum);
for(int i=0;i<c.length();i++){
if(i==0) cout<<d[c[i]-'0'];
else cout<<" "<<d[c[i]-'0'];
}
}