본문 바로가기
Study or Lecture/항해 99클럽 코딩테스트 스터디

항해 99클럽 코딩테스트 스터디 16일차 TIL (자바 / 비기너)

by yoondoo 2024. 11. 12.
728x90

<오늘의 학습 키워드>

- Queue

<오늘의 문제>

제목 : 백준 / 실버5 / 2161번 카드1

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

<풀이>

정답은 더보기

더보기
더보기
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        Queue<Integer> queue = new LinkedList<>();
        for (int i = 1; i <= N; i++) {
            queue.add(i);
        }

        while (!Objects.isNull(queue.peek())) {
            System.out.println(queue.poll());
            queue.add(queue.poll());
        }
    }
}

 

- 맨위에 카드는 버리고 그 다음 카드는 제일 뒤로 보내라고 해서 자료구조 중 하나인 큐를 이용했다.

<결론, 주의할점>

- queue에 null을 넣게 되면 size가 1이라고 판단하기 때문에 다른 방법으로 while의 조건문을 해줘야한다.

반응형

댓글