[백준/C++] 10828번 풀이

업데이트:



문제 정보


풀이

문제

백준 10828번 문제를 풀이합니다.

코드

`cpp #include “iostream” #include “string”

#define STACK_SIZE 10000

using namespace std;

class Stack { private: int stack[STACK_SIZE]; int size = 0;

public: Stack() {}

void push(int n) {
	stack[size++] = n;
}

int pop() {
	return size > 0 ? stack[--size] : -1;
}

int getSize() {
	return size;
}

int empty() {
	return size != 0 ? 0 : 1;
}

int top() {
	return size > 0 ? stack[size - 1] : -1;
}

void print() {
	cout << "[";
	for (int i = 0; i < size; i++) {
		cout << stack[i];
	}
	cout << "]" << endl;
} };

int main() { ios_base::sync_with_stdio(false); Stack stack = Stack();

int n;
cin >> n;
cin.ignore(5, '\n');

for (int i = 0; i < n; i++) {
	string command;
	getline(cin, command);
	
	switch (command.front()) {
	case 'p':
		if (command.at(1) == 'u') {
			stack.push(atoi(command.substr(5).c_str()));
		}
		else {
			cout << stack.pop() << endl;
		}
		break;
	case 's':
		cout << stack.getSize() << endl;
		break;
	case 'e':
		cout << stack.empty() << endl;
		break;
	case 't':
		cout << stack.top() << endl;
		break;
	}
}

return 0; } ```

설명

저장소의 기존 제출 코드를 기준으로 정리한 풀이입니다.



댓글남기기