原题目
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
题目大意
给定一个非负整数,计算这个数各位的和,并用英语输出这个和的每一位数。
题解
注意题目中给出的N可能非常大,所以直接用getchar来读,边读边加。如果N为0时也要单独处理。
C语言代码如下:
#include<stdio.h>
char words[10][6] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main(){
char c;
int sum = 0;
while((c = getchar()) != '\n'){
sum += c - '0';
}
int num[3];
int cnt = 0;
if(!sum){
printf("%s\n", words[0]);
return 0;
}
while(sum > 0){
num[cnt++] = sum % 10;
sum /= 10;
}
for(int i = cnt - 1;i >= 0;--i){
printf("%s", words[num[i]]);
if(i)
printf(" ");
else
printf("\n");
}
return 0;
}