zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) Practice 1005 Spell It Right (20 分) (switch)

    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 (≤).

    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

    思路:求和,英文输出,但是需要注意特殊情况:零的时候。
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <stack>
     7 using namespace std;
     8 string s;
     9 void _print(int x)
    10 {
    11     stack<int> s;
    12     while(x){
    13         s.push(x%10);
    14         x/=10; 
    15     }
    16     while(!s.empty()){
    17         switch(s.top()){
    18             case 0: cout<<"zero"; break;
    19             case 1: cout<<"one";break;
    20             case 2: cout<<"two";break;
    21             case 3: cout<<"three";break;
    22             case 4: cout<<"four";break;
    23             case 5: cout<<"five";break;
    24             case 6: cout<<"six";break;
    25             case 7: cout<<"seven";break;
    26             case 8: cout<<"eight";break;
    27             case 9: cout<<"nine";break;
    28         }
    29         s.pop();
    30         if(!s.empty()) cout<<" ";
    31     }
    32     cout<<endl;
    33 }
    34 int main()
    35 {
    36     while(cin>>s){
    37         int sum=0;
    38         for(int i=0;i<s.length();i++){
    39             sum+=int(s[i]-'0');
    40         }
    41         if(sum==0) cout<<"zero"<<endl;
    42         else _print(sum);
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    ue4 socket
    ue4动画蓝图
    localStorage 用法
    关于textarea中换行、回车、空格的识别与处理
    git忽略某些文件提交
    动态加载js文件
    H5 App页面 绝对定位 软键盘弹出时顶起底部按钮
    Android软键盘弹出时把布局顶上去的解决方法
    javascript 事件委托 和jQuery事件绑定on、off 和one
    escape()、encodeURI()、encodeURIComponent()区别详解
  • 原文地址:https://www.cnblogs.com/wydxry/p/11053264.html
Copyright © 2011-2022 走看看