SOLUTION INFO
C++ · main.cpp
- 작성자
- tony9402
- 공동 작성자
- 없음
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N; cin >> N;
stack<int> st;
for(int i=0;i<N;i++){
string cmd; cin >> cmd;
if(cmd == "push") {
int X; cin >> X;
st.push(X);
}
else if(cmd == "pop") {
if(st.empty()) {
cout << -1 << '\n';
}
else {
cout << st.top() << '\n';
st.pop();
}
}
else if(cmd == "size") {
cout << (int)st.size() << '\n';
}
else if(cmd == "empty") {
cout << st.empty() << '\n';
}
else if(cmd == "top") {
if(st.empty()) {
cout << -1 << '\n';
}
else {
cout << st.top() << '\n';
}
}
}
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Java · Main.java
- 작성자
- tony9402
- 공동 작성자
- 없음
import java.lang.*;
import java.util.*;
import java.io.*;
public class Main{
static public void main(String[] args) {
FastReader rd = new FastReader();
int N = rd.nextInt();
Stack<String> stack = new Stack<>();
StringBuilder out = new StringBuilder();
for(int i=0;i<N;i++){
String[] Line = rd.nextLine().split(" ");
String cmd = Line[0];
if(cmd.equals("push")) {
stack.push(Line[1]);
}
else if(cmd.equals("pop")) {
if(stack.empty()){
out.append("-1");
}
else {
out.append(stack.peek());
stack.pop();
}
out.append("\n");
}
else if(cmd.equals("size")) {
out.append(stack.size() + "\n");
}
else if(cmd.equals("empty")) {
out.append(stack.empty() ? "1" : "0");
out.append("\n");
}
else if(cmd.equals("top")) {
if(stack.empty()){
out.append("-1\n");
}
else {
out.append(stack.peek() + "\n");
}
}
}
System.out.print(out);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Python · main.py
- 작성자
- tony9402
- 공동 작성자
- 없음
import sys
def input():
return sys.stdin.readline().rstrip()
N = int(input())
stack = []
for i in range(N):
cmd = input().split()
X = 0
if len(cmd) == 2:
X = cmd[1]
cmd = cmd[0]
if cmd == "push":
stack.append(X)
elif cmd == "pop":
if len(stack) == 0:
print(-1)
else:
print(stack[-1])
stack.pop(-1)
elif cmd == "size":
print(len(stack))
elif cmd == "empty":
print(0 if len(stack) else 1)
elif cmd == "top":
if len(stack) == 0:
print(-1)
else:
print(stack[-1])
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.