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
题意
输入一串数字,计算每个数字相加后的和,用英文表示
代码
#include <iostream>
#include <string>
using namespace std;
char n[101];
int main() {
cin >> n;
int i = 0,sum = 0;
while(n[i]!='\0') {
sum += n[i]-'0';
i++;
}
//cout << sum << endl;
string s = to_string(sum);
int len;
len = s.length();
string English[10]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
int d[10]= {0}; //存放分割sum的每一位数字
for(int j=0; j<len; j++) {
d[j]=sum%10;
sum=sum/10;
}
for(int j=len-1; j>=0; j--) {
if(j==0) {
cout << English[d[j]] << endl;
} else {
cout << English[d[j]] << " ";
}
}
return 0;
}