PAT-1005 Spell It Right

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

题意

给定字符串,对每个字符进行相加,结果的每个数字然后转化为英文字母

Code

#include<iostream>
#include<string>
using namespace std;

int main(){
    
    
    string n;
    cin>>n;
    string maptable[] = {
    
    "zero","one","two","three","four",
        "five","six","seven","eight","nine","ten"};
    int temp = 0;
    for(int i=0; i<n.size(); i++){
    
    
        temp += n[i]-'0';
    }
    //cout << temp<<endl;
    string res = to_string(temp);
    cout << maptable[res[0]-'0'];
    for(int i=1; i<res.size(); i++){
    
    
        cout <<" "<<maptable[res[i]-'0'];
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42100456/article/details/108742377