目录
题目:
1005 Spell It Right (20)(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
解题思路:
大数存储计算与字符转换问题。用栈的方式保存数值,打印对应值结果。
C语言实现:
#include<stdio.h>
int main(int argc, char** argv) {
char numOfEnglish[10][6]={
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"
};
int num[101]={0}; //计算数组
int count=0; //栈顶
int i;
char temp=NULL;
while((temp=getchar())!='\n'){
num[0]+=temp-'0';
i=0;
while(num[i]>=10){ //当前位置大于10则进位
num[i+1]+=num[i]/10;
num[i]%=10;
++i;
if(i>count)
count=i;
}
}
for(int j=count;j>=0;--j){
printf("%s",numOfEnglish[num[j]]);
if(j>0){
printf(" ");
}
}
return 0;
}