문제 출처:https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net

그저 stack을 구현하는 문제입니다. 따로 c++에서 STL로 제공해주는 함수들이 있으니 활용해봤습니다.
코드는 다음과 같습니다:
#include <iostream>
#include <stack>
using namespace std ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(NULL) ; cout.tie(NULL) ;
stack<int> ss ;
int line_number, num ;
string temp ;
cin >> line_number ;
for(int i = 0 ; i < line_number ; i++){
cin >> temp ;
if(temp.compare("push") == 0){
cin >> num ;
ss.push(num) ;
}
else if(temp.compare("pop") == 0){
if(ss.empty())
cout << "-1" << endl ;
else{
cout << ss.top() << endl ;
ss.pop() ;
}
}
else if(temp.compare("size") == 0)
cout << ss.size() << endl ;
else if(temp.compare("empty") == 0)
cout << ss.empty() << endl ;
else if(temp.compare("top") == 0){
if(ss.empty())
cout << "-1" << endl ;
else
cout << ss.top() << endl ;
}
}
return 0 ;
}
해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/BackJoon/HPS/P10828_Stack.cpp
GitHub - gurcks8989/CodingTest: CodingTest_study_with_c++
CodingTest_study_with_c++. Contribute to gurcks8989/CodingTest development by creating an account on GitHub.
github.com
훈수 및 조언은 언제든 환영입니다.
문제 출처:https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net

그저 stack을 구현하는 문제입니다. 따로 c++에서 STL로 제공해주는 함수들이 있으니 활용해봤습니다.
코드는 다음과 같습니다:
#include <iostream>
#include <stack>
using namespace std ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(NULL) ; cout.tie(NULL) ;
stack<int> ss ;
int line_number, num ;
string temp ;
cin >> line_number ;
for(int i = 0 ; i < line_number ; i++){
cin >> temp ;
if(temp.compare("push") == 0){
cin >> num ;
ss.push(num) ;
}
else if(temp.compare("pop") == 0){
if(ss.empty())
cout << "-1" << endl ;
else{
cout << ss.top() << endl ;
ss.pop() ;
}
}
else if(temp.compare("size") == 0)
cout << ss.size() << endl ;
else if(temp.compare("empty") == 0)
cout << ss.empty() << endl ;
else if(temp.compare("top") == 0){
if(ss.empty())
cout << "-1" << endl ;
else
cout << ss.top() << endl ;
}
}
return 0 ;
}
해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/BackJoon/HPS/P10828_Stack.cpp
GitHub - gurcks8989/CodingTest: CodingTest_study_with_c++
CodingTest_study_with_c++. Contribute to gurcks8989/CodingTest development by creating an account on GitHub.
github.com
훈수 및 조언은 언제든 환영입니다.