PTA——7-32 说反话-加强版

题目描述:

给定一个英语句子,各个单词之间用空格分隔。要求编写程序,将所有单词倒序输出

输入示例:

Hello World Here I Come

输出示例:

Come I Here World Hello

 1 #include <stack>
 2 #include <string>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int main(){
 8     stack<string> v;
 9     string s;
10     while(cin >> s) v.push(s);
11     if(v.empty()) return 0;
12     cout << v.top();
13     v.pop();
14     while(!v.empty()){
15         cout << " " << v.top();
16         v.pop();
17     }
18     return 0;
19 }

  • 一开始3没有通过,添加19行判断栈是否为空即可

猜你喜欢

转载自www.cnblogs.com/cxc1357/p/12219613.html