description
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
idea
- 整数的每一位加一块,用英语读出来和的各位
- 同1001,涉及对每一位的处理,联想字符串。
solution1
#include <stdio.h>
#include <string.h>
char hash[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
void print(int n, int m){
if(n >= 10) print(n / 10, m);
printf("%s", hash[n%10]);
if(n != m) printf(" ");
}
int main(){
char n[101];
int sum = 0, t;
scanf("%s", n);
for(int i = 0; i < strlen(n); i++){
sum += n[i] - '0';
}
print(sum, sum);
return 0;
}
solution2
#include <stdio.h>
#include <string.h>
char hash[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main(){
char n[101];
int sum = 0, t;
scanf("%s", n);
for(int i = 0; i < strlen(n); i++){
sum += n[i] - '0';
}
sprintf(n, "%d", sum);
for(int i = 0; i < strlen(n); i++){
printf("%s", hash[n[i] - '0']);
if(i != strlen(n) - 1) printf(" ");
}
return 0;
}
keep hungry