PAT甲级-1005 Spell It Right (20分)

点击链接PAT甲级-AC全解汇总

题目:
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
using namespace std;

int main()
{
    char c;
    int sum=0;
    while((c=getchar())!='\n')
    {
        sum+=c-'0';
    }
    string str=to_string(sum);
    string num_eng[10]={"zero","one","two","three","four","five",
                        "six","seven","eight","nine"};
    for(int i=0;i<str.length();i++)
    {
        cout<<num_eng[str[i]-'0'];
        if(i<str.length()-1)cout<<" ";
    }

    return 0;
}

注意: 一开始zero拼成了zero害我调了半天。。。

你可能感兴趣的:(PAT)