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;
}

本文介绍了一个算法问题“1005SpellItRight”,任务是将给定的非负整数的每一位数字相加,并将结果的每一位数字用英文单词表示。文章提供了一段C++代码示例,展示了如何通过模拟实现这一转换过程。
663

被折叠的 条评论
为什么被折叠?



