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

PAT 甲级 1005 Spell It Right (20 分)

时间:2019-01-06 11:53:55      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:title   english   pre   car   ever   lse   using   list   NPU   

1005 Spell It Right (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

注意:题中有一个case是0

 1 #include<iostream>
 2 #include<string>
 3 #include<stack>
 4 
 5 using namespace std;
 6 
 7 string numEnglish[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
 8 
 9 int main()
10 {
11   string str;
12   int sum = 0;
13   stack<int> s;
14   
15   getline(cin,str);
16   
17   for(int i=0;i<str.size();++i)
18     sum += str[i]-48;
19     
20   if(sum == 0)
21     cout<<"zero"<<endl;
22   else
23   {
24     while(sum)
25     {
26       s.push(sum%10);
27       sum /= 10;
28     }
29     
30     cout<<numEnglish[s.top()];
31     s.pop();
32     
33     while(!s.empty())
34     {
35       cout << " " << numEnglish[s.top()];
36       s.pop();
37     }
38   }
39   
40   return 0;
41 }

 

 

PAT 甲级 1005 Spell It Right (20 分)

标签:title   english   pre   car   ever   lse   using   list   NPU   

原文地址:https://www.cnblogs.com/cdp1591652208/p/10227486.html

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