码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1005.Spell It Right

时间:2015-02-04 16:10:53      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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
思路:一定要注意数值的取值范围,10^100可能不是longlong 就能解决的问题
技术分享
 1 #include<cstdio>
 2 #include<cstring>
 3 char data[11][10]=
 4 {
 5     "zero",
 6     "one",
 7     "two",
 8     "three",
 9     "four",
10     "five",
11     "six",
12     "seven",
13     "eight",
14     "nine"
15 };
16 char str[200];
17 int main(int argc, char *argv[])
18 {
19     scanf("%s",str);  
20     int sum=0;
21     for(int i=0;str[i]!=\0;i++)
22     {
23         sum+=str[i]-0;
24     }     
25     sprintf(str,"%d",sum);
26     for(int i=0;i<strlen(str);i++)
27     {    
28         int index=str[i]-0;
29         if(i==strlen(str)-1)
30             printf("%s\n",data[index]);             
31         else
32             printf("%s ",data[index]);
33     }
34     return 0;
35 }
View Code

 

PAT1005.Spell It Right

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4272551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!