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 (≤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
思路:用栈来实现 ,将求出的和数从个位依次取出每个数位上的数,压栈,取完之后从栈顶依次出栈,则先出来的数肯定是高位数。15—>5入栈—>1入栈—>1出栈—>5出栈
#include<iostream>
#include<stack>
using namespace std;
int main()
{
string str;
cin>>str;
int count=0;
for(int i=0;i<str.length();i++)
{
count+=(str[i]-'0');
}
stack<int> s;
if(count==0)
cout<<"zero";
while(count!=0)
{
int num=count%10;
s.push(num);
count/=10;
}
while(!s.empty())
{
int t=s.top();
s.pop();
switch(t)
{
case 0:
cout<<"zero";
break;
case 1:
cout<<"one";
break;
case 2:
cout<<"two";
break;
case 3:
cout<<"three";
break;
case 4:
cout<<"four";
break;
case 5:
cout<<"five";
break;
case 6:
cout<<"six";
break;
case 7:
cout<<"seven";
break;
case 8:
cout<<"eight";
break;
case 9:
cout<<"nine";
break;
}
if(!s.empty()) cout<<" ";
}
}