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 (≤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
题目大意:
给定一个非负整数,计算该数的和sum,并把每个数字用英文字母输出。
代码:
把该非负整数看成字符串(char)输入:
#include<stdio.h>
int main(){
int i = 0, t, sum = 0;
int a[10];
char s[100];
char str[10][6] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
scanf("%s", s);
while(s[i] != '\0'){
sum += s[i] - '0';
i++;
}
i = 0;
if(sum == 0){
printf("%s", str[0]);
return 0;
}
while(sum > 0){
t = sum % 10;
a[i++] = t;
sum /= 10;
}
for(int j = i-1; j >= 0; j--){
printf("%s", str[a[j]]);
if(j != 0)
printf(" ");
}
return 0;
}
int类型输入(有一个测试点错误,可能是long long int类型定义的n 不够大,所以换成的字符串输入):
#include<stdio.h>
int main(){
long long int n;
int i = 0, t, sum = 0;
int a[10];
char str[10][6] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
scanf("%lld", &n);
while(n > 0){
t = n % 10;
sum += t;
n /=10;
}
if(sum == 0){
printf("%s", str[0]);
return 0;
}
while(sum > 0){
t = sum % 10;
a[i++] = t;
sum /= 10;
}
for(int j = i-1; j >= 0; j--){
printf("%s", str[a[j]]);
if(j != 0)
printf(" ");
}
return 0;
}