1005 Spell It Right (20 分)

QuestionDescribe
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
结尾无空行

题目分析:将字符串s累加至sum中,再将sum转换为字符串str,最后按顺序输出c[str[i]-‘0’]数组中的内容。

#include<iostream>
using namespace std;

int main(){
    string s;
    cin >> s;
    int sum = 0;
    for(int i = 0; i < s.size(); i ++){
        sum += (s[i] - '0');
    }
    string c[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
    string str = to_string(sum);
    bool flag = false;
    for(int i = 0; i < str.size(); i ++){
        if(flag == false){
            cout << c[str[i] - '0'];
            flag = true;
        }else{
            cout << " " << c[str[i] - '0'];
        }
    }
    return 0;
}
上一篇:微信小程序笔记


下一篇:PAT Advanced 1005 Spell It Right