2022年6月9日 星期四

i213: stack 練習

解題心得

就很基本的stack(?

程式碼

#include <iostream>
#include <stack>
using namespace std;

int main()
{
	int n, k, x;
	stack<int> st;
	
	cin >> n;
	while (n--)
	{
		cin >> k;
		if (k == 1)
		{
			cin >> x;
			st.push(x);
		}
		else if (k == 2)
		{
			if (st.empty()) cout << -1 << endl;
			else cout << st.top() << endl;
		}
		else if (k == 3)
		{
			if (!st.empty())
				st.pop();
		}
	}
	return 0;
}

沒有留言:

張貼留言