PAT 1009 说反话 (20 分)
本题思路不难,关键在于用getline()读入包含空格的整行
#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int main() {
stack<string> s;
string word;
string sentence;
getline(cin, sentence);//读入整行(包含空格)
for (int i = 0; i < sentence.length(); i++) {
string x;
while (sentence[i] != ' '&&i < sentence.length()) {
x += sentence[i];
i++;
}
s.push(x);
}
word = s.top();
s.pop();
cout << word;
while (!s.empty()) {
word = s.top();
s.pop();
cout << " " << word;
}
cout << endl;
return 0;
}