Advanced Leve 1005 Spell It Right (20 point(s))

Theme

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 ( ≤ 1 0 100 ) N (≤10^{100}) 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

思路分析:

利用string中的to_string函数将求和玩的数转换为string类型

代码:

#include <bits/stdc++.h>
using namespace std;
string s, num[] = {
    
    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main(){
    
    
    getline(cin, s);
    int total = 0;
    for(auto c : s) total += c - 48;
    string res = to_string(total);
    for(int i = 0; i < res.length(); i++) printf("%s%s", i ? " " : "", num[res[i] - 48].c_str());
    return 0;
}

PAT_Advanced_Level

猜你喜欢

转载自blog.csdn.net/zy440458/article/details/113813269