专栏文章

405J1R训练四(T628214 日志分析)

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@mioya34y
此快照首次捕获于
2025/12/03 03:07
3 个月前
此快照最后确认于
2025/12/03 03:07
3 个月前
查看原文

错误思路

看不懂题。

正确思路

先定两个变量stack s1;stack s2;再定一个n,输入n再来个while循环,循环中先定一个整数x变量,在输入x,最后来一个if判断和两个else if:
CPP
if(x==0){
	int w;
	cin>>w;
	s1.push(w);
	if(s2.empty()){
		s2.push(w);
	}else if(s2.top()<w){
		s2.push(w);
        }else{
		s2.push(s2.top());
	} 
	}else if(x==1){
		s1.pop();
		s2.pop();	
	}else if(x==2){
		if(s1.empty()){
			cout<<0<<endl;
	         }else{
		        cout<<s2.top()<<endl;
		}
	}

正确代码

CPP
#include<bits/stdc++.h>
using namespace std;
stack<int> s1;
stack<int> s2;
int main(){
	int n;
	cin>>n;
	while(n--){
		int x;
		cin>>x;
		if(x==0){
			int w;
			cin>>w;
			s1.push(w);
			if(s2.empty()){
				s2.push(w);
			}else if(s2.top()<w){
				s2.push(w);
			}else{
				s2.push(s2.top());
			} 
		}else if(x==1){
			s1.pop();
			s2.pop();	
		}else if(x==2){
			if(s1.empty()){
				cout<<0<<endl;
			}else{
				cout<<s2.top()<<endl;
			}
		}
	}
	return 0;
}

评论

0 条评论,欢迎与作者交流。

正在加载评论...