PAT-A1005 Spell It Right 题目内容及题解
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
题目大意
给出一个数字,计算其各位数字的和并将其各位数字用英文输出。
解题思路
- 用字符串读入数字(有可能长达100位);
- 计算各位数字和;
- 按格式输出答案并返回0值。
代码
#include<stdio.h>
#define MAXN 110
char num[MAXN];
int sum=0;
void print(int n){
char out[][7]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int res[MAXN];
int end;
do{
res[end++]=n%10;
n/=10;
}while(n);
while(end>0){
printf("%s",out[res[--end]]);
if(end>0){
printf(" ");
}else{
printf("\n");
}
}
return;
}
int main(){
int i=0;
fgets(num,MAXN,stdin);
while(num[i]>='0'&&num[i]<='9'){
sum+=(num[i]-'0');
i++;
}
print(sum);
return 0;
}
运行结果