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:
12345
Sample Output:
one five
题目是要求输出一段数字,将各个数字相加得出的和,按位对应输出zero~nine
思路:由于输入的数可能大于长整型,因此选择用一个字符数组来存储,然后循环相加,最后对和循环按10取模,替换成相应的英文,需要注意的是定义的字符数组大小不能太小,输入为0的时候也要能输出zero。
AC代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char number[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char result[10][10] = {0};
int main(int argc, char* argv[])
{
int sum = 0, i;
char num[1000] = {0};
scanf("%s", num);
for(i = 0; i < strlen(num); i++)
{
sum += num[i] - '0';
}
if(sum == 0)
{
printf("zero\n");
}
else{
i = 0;
while(sum){
strcpy(result[i++], number[sum%10]);
sum = sum / 10;
}
for(i = i - 1;i >= 0 ; i--)
{
if(0 == i)
printf("%s\n", result[0]);
else
printf("%s ", result[i]);
}
}
system("pause");
return 0;
}