1005 Spell It Right (20分)
作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
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
题意:
输入一个非负整数,用英文表示该整数每位相加之和,如:12345,各项相加1+2+3+4+5=15,15用英文输出就是one five。
思路:
由于输入为一个很长的整数,我们用字符串记录。
参考代码:
#include <iostream>
#include <string>
using namespace std;
string English[10]={"zero","one","two","three","four",
"five","six","seven","eight","nine"};
int main() {
string str;
cin>>str;
int sum=0;
for(int i=0;i<str.length();i++){
sum+=str[i]-'0';
}
str=to_string(sum);
int len =str.length();
for(int i=0;i<len;i++){
if(i<len-1) cout << English[str[i]-'0']<<" ";
else cout<<English[str[i]-'0']<<endl;
}
return 0;
}
如有错误,欢迎指正