#45828: 看偽建中的解題報告傻眼到下巴掉下來吧


1121226@stu.wghs.tp.edu.tw (Arthur✨EC✨小蜜楓)

學校 : 臺北市私立薇閣高級中學
編號 : 252772
來源 : [60.248.154.139]
最後登入時間 :
2025-05-14 12:52:05
i213. stack 練習 | From: [60.248.154.143] | 發表日期 : 2025-04-18 09:43

#include <bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin>>n;
	stack<int> s;
	while(n--){
	    int k=0;
	    cin>>k;
	    if(k==1){
	        int m;
	        cin>>m;
	        s.push(m); // 將m輸入推疊 
	    }
	    else if (k==2){
	        if(s.empty()){ // 如果堆疊為空
	            cout<<-1<<endl; 
	        }
	        else{
	            cout<<s.top()<<endl; // 輸出堆疊s的頂端
	        }
	    }
	    else {
	        if(s.empty()){
	            continue;
	        }
	        else{
	            s.pop(); // pop出堆疊的頂端
	        }
	    }
	}
	return 0;
}
 
ZeroJudge Forum