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 (≤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
题目大意
给一个非负数,我们需要得到这个数每一位相加的和,输出结果的每位数用英文表示,每个数字用空格隔开。
题目分析
考虑到输入数字的大小限制为10100,所以考虑使用字符数组存储输入数据,将数组的每一位转换为数字进行累加,最后将输出结果以英文的形式输出。
代码
#include "stdio.h"
#include "stdlib.h"
int main(){
char N[105];//存储输入数据
int a[5];
int num;
int sum = 0,total;
int i;
scanf("%s",N);
for (i = 0;N[i] != '\0';i++){
num = N[i]-'0';
sum += num;
}//将输入数字的每一位数字进行累加。
total = sum;
for (i = 0;total != 0;i++){
a[i] = total%10;
total = total/10;
}//将输出结果按数位展开。
if (sum == 0){
i = 1;
a[0] = 0;
}
for (i = i-1;i >=0;i--){
if (a[i] == 0) printf("zero");
if (a[i] == 1) printf("one");
if (a[i] == 2) printf("two");
if (a[i] == 3) printf("three");
if (a[i] == 4) printf("four");
if (a[i] == 5) printf("five");
if (a[i] == 6) printf("six");
if (a[i] == 7) printf("seven");
if (a[i] == 8) printf("eight");
if (a[i] == 9) printf("nine");
if (i != 0) printf(" ");//按照输出要求格式输出。
}
return 0;
}