PAT甲级 A1005 Spell It Right
随手记录,只希望能帮助到同样遇到问题的小伙伴呀~
还是学生,水平有限,写的不好的地方有请指正~
这题没什么东西,只是需要注意在输入0的时候应该输出为zero
先上代码~
代码
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main() {
char t;
int res = 0;
string word[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
while ((t = getchar()) != '\n') {
res += t - 48;
}
stack<string>stk;
if (res == 0) {//输入0的时候压入一个zero
stk.push(word[0]);
}
while (res != 0) {
stk.push(word[res % 10]);
res /= 10;
}
while (!stk.empty()) {
cout << stk.top();
stk.pop();
if (!stk.empty()) {
cout << " ";
}
}
cout << endl;
return 0;
}
本文分享了PAT甲级A1005 Spell It Right的问题解决方法,主要涉及数字字符串转换为英文单词的逻辑。代码中使用了栈来处理数字,并特别注意了输入0时应输出'zero'。通过实例代码解释了数字到英文单词的转换过程。
717

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



