zoukankan      html  css  js  c++  java
  • PAT 1005 Spell It Right

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

    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<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    vector<string> vec={{"zero"},{"one"},{"two"},{"three"},{"four"},{"five"},{"six"},{"seven"},{"eight"},{"nine"}};
    
    vector<int> temp;
    
    
    int main(){
        string s;
        cin >> s;
        ll sum = 0;
        for(int i=0;i < s.size();i++){
            sum += (s[i]-'0');
        }
        if(sum == 0) cout << vec[0];
        while(sum){
            temp.push_back(sum%10);
            sum = sum/10;
        }
        for(int i=temp.size()-1;i>=0;i--){
            cout << vec[temp[i]];
            if(i) cout << " ";
        }
    
    
        return 0;
    }

    sum = 0的情况要注意,不会进入循环。

  • 相关阅读:
    Android笔记
    Scala中apply的用法
    MySQL备忘
    Spring test
    Scala
    Dubbo
    Scala元组
    Scala中None, Nil, Nothing的区别
    java多态与异常处理——动手动脑
    《大道至简》第七八章读后感
  • 原文地址:https://www.cnblogs.com/cunyusup/p/10674575.html
Copyright © 2011-2022 走看看