백준 10828번 : 스택

inxnxng·2021년 1월 28일
0

알고리즘 공부

목록 보기
17/42
post-thumbnail

링크 : https://www.acmicpc.net/problem/10828

왜 계속 쉬운거 해요?
-> 피곤해서.. 빨리.. 해결할 수 있는 거 하고 있습니다..

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

int main() {
	int num, elem;
	string word;
	stack<int> stk;
	
	cin >> num;

	for (int i = 0; i < num; i++) {
		cin >> word;
		if (word == "push") {
			cin >> elem;
			stk.push(elem);
		}
		else if (word == "pop") {
			if (stk.empty())
				cout << -1 << endl;
			else {
				elem = stk.top();
				cout << elem << endl;
				stk.pop();
			}
		}
		else if (word == "size") {
			cout << stk.size() << endl;
		}
		else if (word == "empty") {
			if (stk.empty())
				cout << 1 << endl;
			else
				cout << 0 << endl;
		}
		else {
			if (stk.empty())
				cout << -1 << endl;
			else{
				elem = stk.top();
				cout << elem << endl;
			}
		}
	}
	return 0; 
}

왜 switch문으로 안했나요? 그게 더 깔끔한데 코알못이네요?
-> 그 컴퓨터 구조에서 switch문을 컴파일러가 if문으로 일일이 푼다고 배웠습니다. 그래서 컴퓨터 생각해서 if문으로 썼습니다..

자료구조때 char로 잘 구현했으면서 왜 계속 string 쓰세요?
-> 좋은 길 내비두고 왜 어려운 길로 가나요.. 그래도 필요할 때에는 char 잘 씁니다.. 자료구조 때 열심히 썼어요..

0개의 댓글