文章
19
粉丝
0
获赞
125
访问
3.0k
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
vector<string> words;
string word;
for(char c : s) {
if(c == ' ') {
words.push_back(word);
word.clear();
} else word += c;
}
words.push_back(word);
for (int i = words.size() - 1; i >= 0; i--) {
cout << words[i];
if (i > 0) {
cout << " "; // 单词之间用空格分隔
}
} return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
stack<string> st;
string word;
for(char c : s) {
if(c == ' ') {
st.push(word); //若遇到空格则拼成完整单词,压栈
word.clear(); //格式化,清空单词
} else word += c; //单词拼接
}
st.push(word); //最后一个单词没有末尾空格,故需处理
while (!st.empty()) {
cout << st.top(); // 输出栈顶单词
st.pop(); // 弹出栈顶单词
if (!st.empty()) {
cout << " "...
登录后发布评论
暂无评论,来抢沙发