[큐 _ 10845]
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Problem_10845 {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
Scanner scanner = new Scanner(System.in);
int operationNum = scanner.nextInt();
for(int i=0; i<operationNum; ++i){
String input = scanner.next();
switch (input){
case "push":
int num = scanner.nextInt();
queue.offer(num);
break;
case "pop":
System.out.println(queue.isEmpty() ? -1 : queue.poll());
break;
case "size":
System.out.println(queue.size());
break;
case "empty":
System.out.println(queue.isEmpty() ? 1 : 0);
break;
case "front":
System.out.println(queue.isEmpty() ? -1 : queue.peek());
break;
case "back":
if(queue.isEmpty()) System.out.println(-1);
else {
int lastNum = ((LinkedList<Integer>) queue).getLast();
System.out.println(lastNum);
}
break;
}
}
}
}
[카드2 _ 2164]
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Problem_2164 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
Queue<Integer> queue = new LinkedList<>();
for(int i=1; i<=N; ++i)
queue.offer(i);
while (queue.size() > 1){
//맨 앞을 pop
queue.poll();
//맨 앞의 숫자를 맨 뒤로 보내기
int frontNum = queue.poll();
queue.offer(frontNum);
}
System.out.println(queue.poll());
}
}
'Algorithm > Problem_백준' 카테고리의 다른 글
이분 탐색(1920, 10816) (0) | 2019.11.26 |
---|---|
분할 정복(2630, 1992) (0) | 2019.11.21 |
스택(10773, 4949) (0) | 2019.11.05 |
수학3 (5086, 1037) (0) | 2019.10.31 |
그리디 알고리즘(11047, 1931) (0) | 2019.10.29 |