美文网首页PAT甲级
PAT 1005 Spell It Right (20 分)

PAT 1005 Spell It Right (20 分)

作者: zju_dream | 来源:发表于2019-06-06 20:16 被阅读0次

    PAT 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 (≤10^{100}).

    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<string>
    #include<iostream>
    #include<vector>
    
    using namespace std;
    
    int getSum(string str) {
        int res = 0;
        for (int i = 0; i < str.size(); ++i)
        {
            res += str[i] - '0';
        }
        return res;
    }
    
    string num2str[10] = {"zero", "one", "two", "three", "four", 
                            "five", "six", "seven", "eight", "nine"};
    
    
    void sum2vec(vector<string> &v, int sum) {
        if(sum == 0) v.push_back("zero");
        while(sum != 0) {
            v.push_back(num2str[sum%10]);
            sum /= 10;
        }
    }
    
    int main() {
        string str;
        cin >> str;
        int res = getSum(str);
        vector<string> v;
        sum2vec(v, res);
    
        for (int i = v.size()-1; i >= 0; --i)
        {
            cout << v[i];
            if(i != 0) cout << " ";
        }
    }
    

    相关文章

      网友评论

        本文标题:PAT 1005 Spell It Right (20 分)

        本文链接:https://www.haomeiwen.com/subject/iexpxctx.html