cpp) 백준 10828: 스택
본문 바로가기
코딩 테스트/백준 (C++, Python)

cpp) 백준 10828: 스택

by NEWSUN* 2023. 7. 8.

Problem

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

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

 

Solution


  
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int s=0; // stack의 size
int stack[10001];
void push(int k){
stack[s] = k;
s++;
}
int pop(){
if(s==0)
return -1;
s--;
return stack[s];
}
int size(){
return s;
}
int empty(){
if (s==0)
return 1;
else
return 0;
}
int top(){
if (s==0)
return -1;
return stack[s-1];
}
int main(){
int n;
cin >> n;
while(n--){
string str;
cin >> str;
if (str == "push"){
int k;
cin >> k;
push(k);
}
if (str == "pop"){
cout << pop() << endl;
}
if (str == "size"){
cout << size() << endl;
}
if (str == "empty"){
cout << empty() << endl;
}
if (str == "top"){
cout << top() << endl;
}
}
return 0;
}

 

'코딩 테스트 > 백준 (C++, Python)' 카테고리의 다른 글

cpp) 백준 18258: 큐 2  (0) 2023.07.12
cpp) 백준 10773: 제로  (0) 2023.07.08
cpp) 백준 2606: 바이러스  (0) 2023.07.05
cpp) 백준 1012: 유기농 배추  (0) 2023.07.04
cpp) 백준 11723: 집합  (0) 2023.07.01