题解 | #句子逆序#
句子逆序
https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
#include <iostream>
#include <stack>
using namespace std;
int main()
{
string input, new_str;
stack<string> s; //使用string(单词)组成的stack实现逆序输出的效果
while (cin >> input) //while循环中cin遇到空格和回车均不会结束
{
s.push(input);
if (cin.get() == '\n') //使用get()判断是否遇到空格
break;
}
while (!s.empty())
{
cout << s.top();
s.pop();
if (!s.empty())
cout << " ";
}
return 0;
}
查看6道真题和解析