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; }
相关文章推荐
- android发送短信代码(短信内容超长处理)
- 双重指针
- 初步学习Protected Mode(2)
- Control Groups 实验笔记
- 中央处理器CPU
- MyEclipse无法在tomcat部署web项目的可能原因
- 单链表头结点未知的情况下删除任意节点
- 爱立信软件+服务收入占67% CEO称转型到了关键点
- 2亿个整数中求最大的100万之和
- hbase 写操作参数优化
- tomcat7--container
- Revit API选择三维视图上一点
- 高效jQuery
- PHP性能优化工具–xhprof安装
- 寻找最大的k个数
- 解决jquery mobile的header和footer在点击屏幕的时候消失的办法
- 计算机基本概念
- URAL 1586. Threeprime Numbers
- ArrayAdapter
- Android Studio 快捷键盘