1005 Spell It Right (20 分)

Given a non-negative integer NNN, your task is to compute the sum of all the digits of NNN, 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 NNN (≤10100\le 10^{100}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

给一端长度最多为101的数字串,每个位置的数字相加后将其结果用英文表示(把结果的每位数字用英文替换)

思路:模拟

Code

#include <bits/stdc++.h>

using namespace std;

stack<string> stk;
string change[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

int main() {
  string num;
  long long sum = 0;
  cin >> num;
  for (int i = num.size() - 1; i >= 0; i--) {
    sum += num[i] - '0';
  }
  while (!stk.empty()) {
    stk.pop();
  }
  do {
    stk.push(change[sum % 10]);
    sum /= 10;
  }while(sum);
  cout << stk.top();
  stk.pop();
  while (!stk.empty()) {
    cout << " " << stk.top();
    stk.pop();
  }
  cout << endl;
  return 0;
}