文章

27

粉丝

86

获赞

10

访问

21.0k

头像
堆栈的使用 题解:C++ stack
P1372 吉林大学机试题
发布于2023年9月4日 19:23
阅读数 721

C++

#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
int main(){
	int n;
	string str;
	while(cin >> n){
		if(n == 0){
			break;
		}
		stack<int> s;
		int num;
		cin.ignore();
		for(int i = 0; i < n; i++){
			getline(cin, str);
			if(str.substr(0, 1) == "P"){
				num = stoi(str.substr(2));
				s.push(num);
			}
			else if(str.substr(0, 1) == "O"){
				if(s.empty()){
					continue;
				}
				else{
					s.pop();
				}
			}
			else if(str.substr(0, 1) == "A"){
				if(s.empty()){
					cout << 'E' << endl;
				}
				else{
					cout << s.top() << endl;
				}
			}
		}
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发