您的位置:首页 > 其它

PAT 1005. Spell It Right (20)

2014-02-25 18:24 501 查看


1005. Spell It Right (20)

时间限制

400 ms

内存限制

32000 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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


提交代

#include<stdio.h> //欢迎交流学习,如果你觉得对你有帮助,希望回复一下~~~
#include<string.h>
const int MAX=1000000;
char str[MAX];  //保存输入的数字串
int ans[MAX];   //把和分成 各个字符
int length,sum=0; //sum保存各位的累加和
char* digit2Eng(int num)   //返回字符串用char*
{
if(num==0) return "zero";
if(num==1) return "one";
if(num==2) return "two";
if(num==3) return "three";
if(num==4) return "four";
if(num==5) return "five";
if(num==6) return "six";
if(num==7) return "seven";
if(num==8) return "eight";
if(num==9) return "nine";
return "NULL";  //为了严谨性~~
}
int main()
{
while(scanf("%s",str)!=EOF){
length=strlen(str); //如abcd就为4个字符。字符长度为有效字符长度,不包括最后的\0
//printf("%d",length);
int i,size=0;
for(i=0;i<length;i++)
sum=sum+str[i]-'0'; //把字符转换成数字
do   //此处用do-while以免漏了0.   把sum拆成各个数字保存在ans[]中
{
ans[size++]=sum%10;
sum=sum/10;
}while(sum!=0);
for(i=size-1;i>=0;i--){
if(i!=size-1) printf(" ");
printf("%s",digit2Eng(ans[i]));
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: