题目描述
英文
中文大意
输入一个非负整数 N 输入它的各个位数的和, 并且按高位到低位的顺序以英文展示
注意:N <= 10100 显然它的位数是 <= 101 的(102 <= 3位)
样例
思路分析
注意点有两个
- 输入N为非负整数 说明有可能输入为 0 需要处理特情
- 计算后需要从高位到低位按需输出——需要使用 栈 引入stack
code
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
int main() {
string convert[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
stack<int> stack_res;
char num[101];
int res = 0;
cin >> num;
for(int i = 0;i < strlen(num);i++) {
res += num[i] - '0';
}
// 特情
if (res == 0) cout << convert[res];
while (res != 0) {
int index = 0;
index = res % 10;
res /= 10;
stack_res.push(index);
}
// 因为要逆序输出 所以使用 stack
while (!stack_res.empty()) {
cout << convert[stack_res.top()];
stack_res.pop();
if (!stack_res.empty()) cout << " ";
}
return 0;
}