PAT 1009 说反话
1. 题目描述及在线测试位置
1.1 将句中所有单词的顺序颠倒输出:‘Here I Come’ 经过处理后就是 ‘Come I Here’
1.2 在线测试位置:PAT 1009 说反话
2. 具体实现
2.1 基本思路
句子中的每个单词按空格进行分割 ‘Here I Come’ ,按要求倒序输出 ‘Come I Here’。整个流程遵循后入先出的原则,使用堆栈进行数据的存储和打印
2.2 AC代码
#include <stack>
#include <iostream>
using namespace std;
int main()
{
stack<string> str;
string word;
while(cin>>word)
{
str.push(word);
//if(getchar()=='\n') break; //cin有返回值,在线调试时,当输入回车,while循环会终止;而本地调试时,会一直处于等待输入状态,因此加上if语句,用于识别回车、退出while循环
}
cout<<str.top();
str.pop();
while(!str.empty())
{
cout<<" "<<str.top();
str.pop();
}
return 0;
}