利用栈 能很快解决该问题
#include <iostream>
#include <stack>
using namespace std;
#include <string>
int main()
{
stack<string>str_stack;
string s;
cin >> s;
str_stack.push(s);
while (getchar() != '\n') {
cin >> s;
str_stack.push(s);
}
//依次出栈 打印栈顶元素
while (!str_stack.empty()) {
cout << str_stack.top() << " ";
str_stack.pop();
}
return 0;
}