Analysis of Pat computer problems in Zhejiang University 1005. spell it right (20)

Source: Internet
Author: User
1005. spell it right (20) Time Limit 400 MS
The memory limit is 32000 kb.
Code length limit: 16000 B
Criterion author 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;} 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.