浙江大学PAT上机题解析之1005. Spell It Right (20)

1005. Spell It Right (20)

时间限制 
400 ms
内存限制 
32000 kB
代码长度限制 
16000 B
判题程序   
Standard
作者   
CHEN, Yue
 

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
 
#include<iostream>
#include <string>
#include <stack>

using namespace std;

string  getString(int n)
{
  switch(n)
  {
  case 0:return "zero";break;
  case 1:return "one";break;
  case 2:return "two";break;
  case 3:return "three";break;
  case 4:return "four";break;
  case 5:return "five";break;
  case 6:return "six";break;
  case 7:return "seven";break;
  case 8:return "eight";break;
  case 9:return "nine";break;

  }
}


int main()
{
  string   str;       
  string::iterator it;
  int flag=false;
  stack<int>   s;
  int sum=0;
  cin>>str;
  for (it=str.begin();it!=str.end();it++)
    sum += (*it)-'0';  
    while(sum)
  {
    s.push(sum%10);
    sum/=10;
  }
  if (s.empty())
      cout<<"zero";

  while(!s.empty())
  {
    if (flag)
    cout<<" ";
    else
    flag = true;

     cout<<getString(s.top());
     s.pop();  
  }
  cout<<endl;
 
  //system("pause");
  return 0;
} 


原文地址:https://www.cnblogs.com/ainima/p/6331280.html