PATA1005题解

 1 #include<iostream>
 2 #include<cstring>
 3 #include<stdio.h>
 4 #include<vector>
 5 #include<queue>
 6 #include<stack>
 7 using namespace std;
 8 string alpha[10] = {"zero", "one", "two", "three", "four", "five", "six" , "seven", "eight", "nine"};
 9 stack<string>ans;
10 void add(string num) {
11     int res = 0;
12     for(int i = 0; i < num.length(); i++) {
13         res += (num[i] - '0');
14     }
15     if(res == 0) {   //一个临界条件,因为数据可能包含0的情况,但是下面的while循环如果是0就直接跳出了
16         cout << "zero";
17         return;
18     }
19     while(res) {
20         ans.push(alpha[res%10]);
21         res /= 10;
22     }
23     while(!ans.empty()) {
24         cout << ans.top();
25         ans.pop();
26         if(ans.size() != 0) cout << " ";  //要写在pop之后
27     }
28 }
29 int main () {
30     string str;
31     cin >> str;
32     add(str);
33 }

 

posted @ 2019-01-30 19:51  奥利奥王子饼干  阅读(186)  评论(0编辑  收藏  举报