PAT 甲级 1005 Spell It Right (20 分)

Posted cdp1591652208

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 甲级 1005 Spell It Right (20 分)相关的知识,希望对你有一定的参考价值。

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 分)的主要内容,如果未能解决你的问题,请参考以下文章

PAT 甲级 1005 Spell It Right (20 分)

PAT甲级1005 Spell It Right

PAT甲级第五题--1005 Spell It Right

菜鸟记录:c语言实现PAT甲级1005--Spell It Right

PAT 1005. Spell It Right

Pat1005:Spell It Right