컴퓨터기본/문제풀이

[백준] 10828번, 1991번

차가운오미자 2021. 6. 14. 22:05

10828번

#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int main(void) {

	stack<int> s;
	vector<int> answer;
	
	int num;
	cin >> num;
	cin.ignore();
	
	string order;
	int elem;
	for (int i = 0; i < num; i++) {
		cin >> order;
		//cout << order << endl;
		
		if (order == "push") {
			cin >> elem;
			s.push(elem);
			//cout << elem << endl;
		}
		else if (order == "pop") {
			if (!s.empty()) {
				answer.push_back(s.top());
				s.pop();
			}
			else {
				answer.push_back(-1);
			}
		}
		else if (order == "size") {
			answer.push_back(s.size());
		}
		else if (order == "empty") {
			if (s.size() == 0) {
				answer.push_back(1);
			}
			else {
				answer.push_back(0);
			}
		}
		else if (order == "top") {
			if (!s.empty()) {
				answer.push_back(s.top());
			}
			else {
				answer.push_back(-1);
			}
		}
		else {
			cout << "error!" << endl;
		}
	}

	for (vector<int>::iterator it = answer.begin(); it != answer.end(); it++) {
		cout << *it << endl;
	}
	

	return 0;
}

 

1991번

#include <iostream>

using namespace std;

int num;

struct node {
	char left;
	char right;
};

struct node arr[27];

void preOrder(char root) {
	if (root == '.')
		return;

	cout << root;

	preOrder(arr[root].left);
	preOrder(arr[root].right);
}

void inOrder(char root) {
	if (root == '.') {
		return;
	}

	inOrder(arr[root].left);
	cout << root;
	inOrder(arr[root].right);
}

void postOrder(char root){
	if(root == '.'){
		return;
	}
	
	postOrder(arr[root].left);
	postOrder(arr[root].right);
	cout << root;
}

int main(void) {
	
	cin >> num;

	char a, b, c;
	for (int i = 1; i <= num; i++) {
		cin >> a >> b >> c;
		arr[a].left = b;
		arr[a].right = c;

	}

	preOrder('A');
	cout << endl; 
	inOrder('A');
	cout << endl;
	postOrder('A');
	cout << endl;

}

'컴퓨터기본 > 문제풀이' 카테고리의 다른 글

[백준] 10870번, 10872번, 2798번  (0) 2021.06.14
[이것이~] 15. 구현  (0) 2021.06.14
[이것이~] 탐욕법, 구현  (0) 2021.06.14
[프로그래머스] 정렬 > K번째수  (0) 2021.06.14
[백준] 2750번, 1181번, 1431번  (0) 2021.06.14