BOJ/[ BOJ ] C++

[ C++ ] #10828 스택

haena02 2022. 12. 21. 02:35
반응형

 

어려운문제는 아니다!

하지만 출력을 예제처럼 깔끔하게 하려고 저장해놨다가 출력했더니 계속 틀렸다.

질의응답을 봤더니 다들 그냥 바로 출력했길래 나도 그렇게 했더니 맞았다..ㅎㅎ

 

#include <iostream>
#include<string>
#include <stack>

using namespace std;

int main() {

	int n,b;
	string a;
	stack<int> s;

	int* result = new int[n];

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a;
		if (a == "push") {
			cin >> b;
			s.push(b);
		}else if(a == "top") {
			if (s.empty()) cout << -1<<"\n";
			else cout<<s.top()<<"\n";
		}
		else if (a == "size") {
			cout << s.size() << "\n";
		}
		else if (a == "empty") {
			if (s.empty()) cout << 1 << "\n";
			else cout << 0 << "\n";
		}
		else {
			if (s.empty()) cout << -1 << "\n";
			else {
				cout << s.top() << "\n";
				s.pop();
			}
		}
		
	}
}
반응형