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 }
     
  • 相关阅读:
    第5.3课.多输入之多线程和fork
    第5.2课多输入之select
    第5.1课,多输入之轮询
    第4课.编写通用的Makefile
    第3课.电子书框架
    2.3freetype矢量字体
    建立u-boot,内核的SI工程
    2.1/2.2字符的编码方式及显示
    1.0数码相框框架分析
    [数据结构]一些有意思题目(一)
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/10227486.html
Copyright © 2011-2022 走看看