zoukankan      html  css  js  c++  java
  • 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 (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

    注意:题中有一个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 }
     
  • 相关阅读:
    第四章、Vue组件
    第三章、Vue实例
    第二章、Vue指令
    Python练习实例012
    Python练习实例011
    Python练习实例010
    Python练习实例009
    Python练习实例008
    Python练习实例007
    Python练习实例006
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/10227486.html
Copyright © 2011-2022 走看看