PAT 甲级 1005 Spell It Right

https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336

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 <bits/stdc++.h>
using namespace std;

char s[1111], num[1111];
char a[20][50] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

void itoa(int x) {
    if(x == 0) {
        num[0] = '0';
        num[1] = 0;
        return ;
    }
    stack<int> st;
    while(x) {
        st.push(x % 10);
        x = x / 10;
    }
    int sz = 0;
    while(!st.empty()) {
        num[sz++] = (char)(st.top() + '0');
        num[sz] = 0;
        st.pop();
    }
}

int main() {
    scanf("%s", s);
    int len = strlen(s);
    int sum = 0;
    for(int i = 0; i < len; i ++) {
        sum += s[i] - '0';
    }

    itoa(sum);
    int lenum = strlen(num);

    for(int i = 0; i < lenum; i ++) {
        printf("%s", a[num[i] - '0']);
        printf("%s", i != lenum - 1 ? " " : "
");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zlrrrr/p/9420593.html