【小白】 acm练习之路

acm 1062反转问题

**#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int main(){
	//利用栈的思想解决这个问题,把每一个保存在栈中,然后遇到空格时就输出
	int n;
	char c;
	cin>>n;
	getchar();
	while(n--){
		stack<char> s;
		while(true){
		 c=getchar();
			if(c!=' '&&c!='\n'&&c!=EOF){
				s.push(c);
			}else {
				while(!s.empty()){
					cout<<s.top();
					s.pop();
			      }
			    
			      if(c=='\n'||c==EOF){
			      	break;
				  }
				    cout<<" ";
				
			}
		}
		cout<<endl;
	} 
	return 0;
}**

分析:利用栈的思想解决这个问题,把每一个单词保存在栈中,然后遇到空格时就输出,然后继续保存继续输出,知道换行为止。

发布了27 篇原创文章 · 获赞 17 · 访问量 176

猜你喜欢

转载自blog.csdn.net/weixin_42918559/article/details/103742996