[PTA] PAT(A) 1005 Spell It Right (20分)

Problem

Description

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

Each input file contains one test case. Each case occupies one line which contains an $N$($\leq10^{100}$).

Output

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

Sample Input

12345

Sample Output

one five

Solution

Analysis

The number of a given value of each bit all together, the final result output in English.
Adding up the value of each one can be read directly into a line, a character and a character processing, a character can be read directly, processing a character.
English final result outputs in the array, each using a digital number corresponding English storage as a subscript. The final result is converted to a string, and a character processing

Code

#include <bits/stdc++.h>
using namespace std;

string word[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

void print_english(int x) {
    string str = to_string(x);
    for (int i = 0; i < str.size(); i++) {
        if (i) cout << " ";
        cout << word[str[i] - '0'];
    }
    cout << endl;
}

int main(void) {
    char ch;
    int sum = 0;
    while (cin >> ch) {
        int x = ch - '0';
        sum += x;
    }
    print_english(sum);
}

Result

Guess you like

Origin www.cnblogs.com/by-sknight/p/11403501.html