일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- UI한글변경
- 장고란
- 표준 입출력
- 알고리즘 공부방법
- 시간복잡도
- double ended queue
- scanf
- 입/출력
- EOF
- 엑셀
- string 함수
- 2557
- 매크로
- Django Nodejs 차이점
- 이분그래프
- iOS14
- getline
- 구조체와 클래스의 공통점 및 차이점
- vscode
- 입출력 패턴
- c++
- 자료구조
- 프레임워크와 라이브러리의 차이
- Django의 편의성
- string 메소드
- k-eta
- 연결요소
- 백준
- correlation coefficient
- Django란
Archives
- Today
- Total
Storage Gonie
(1) [C++, Java] 백준 No.10866 : 덱(기본구현) 본문
반응형
문제
풀이
# C++(직접구현)
심화과정을 거치고 난 뒤 할 것
# C++(STL 사용)
#include <iostream>
#include <deque>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
deque<int> deq;
int N;
cin >> N;
while(N--)
{
string cmd;
cin >> cmd;
if (cmd == "push_front")
{
int num;
cin >> num;
deq.push_front(num);
}
else if (cmd == "push_back")
{
int num;
cin >> num;
deq.push_back(num);
}
else if (cmd == "pop_front")
{
if (!deq.empty()){
cout << deq.front() << endl;
deq.pop_front();
}
else
cout << -1 << endl;
}
else if (cmd == "pop_back")
{
if (!deq.empty()){
cout << deq.back() << endl;
deq.pop_back();
}
else
cout << -1 << endl;
}
else if (cmd == "front")
{
if (!deq.empty())
cout << deq.front() << endl;
else
cout << -1 << endl;
}
else if (cmd == "back")
{
if (!deq.empty())
cout << deq.back() << endl;
else
cout << -1 << endl;
}
else if (cmd == "size")
cout << deq.size() << endl;
else if (cmd == "empty")
cout << deq.empty() << endl;
}
}
# Java
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
for (int k=0; k<n; k++) {
String line = sc.nextLine();
String[] s = line.split(" ");
String cmd = s[0];
if (cmd.equals("push_front")) {
int num = Integer.parseInt(s[1]);
queue.offerFirst(num);
} else if (cmd.equals("push_back")) {
int num = Integer.parseInt(s[1]);
queue.offerLast(num);
} else if (cmd.equals("front")) {
if (queue.isEmpty()) {
System.out.println("-1");
} else {
System.out.println(queue.peekFirst());
}
} else if (cmd.equals("size")) {
System.out.println(queue.size());
} else if (cmd.equals("empty")) {
if (queue.isEmpty()) {
System.out.println("1");
} else {
System.out.println("0");
}
} else if (cmd.equals("pop_front")) {
if (queue.isEmpty()) {
System.out.println("-1");
} else {
System.out.println(queue.pollFirst());
}
} else if (cmd.equals("pop_back")) {
if (queue.isEmpty()) {
System.out.println("-1");
} else {
System.out.println(queue.pollLast());
}
} else if (cmd.equals("back")) {
if (queue.isEmpty()) {
System.out.println("-1");
} else {
System.out.println(queue.peekLast());
}
}
}
}
}
반응형
Comments