题目:
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:12345Sample Output:
one five
题解:
简单的模拟题。
代码:
#include <cstdio>
#include <cstring>
using namespace std;
char word[][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main()
{
char str[110];
scanf("%s", str);
int len = strlen(str), sum = 0;
for(int i = 0; i < len; i++) //计算出各位之和
sum += str[i] - '0';
int ans[5], k = 0;
while(sum) //将sum的每一位逆序保存在ans数组中
{
ans[k++] = sum % 10;
sum /= 10;
}
printf("%s", word[ans[--k]]);
for(int i = k - 1; i >= 0; i--)
printf(" %s", word[ans[i]]);
return 0;
}