본문 바로가기

Coding Test/백준

[백준/자바] 2164 - 카드2

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int result;

        if(n == 1) result = 1;
        else if(n <= 3) result = 2;
        else {
            Queue<Integer> queue = new LinkedList<>();
            for(int i=1; i<=n; i++) queue.add(i);

            while(queue.size() > 1){
                queue.remove();
                int temp = queue.poll();
                queue.offer(temp);
            }
            result = queue.peek();
        }
        System.out.println(result);

        br.close();
    }
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static void main(String[] args) throws IOException {
        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(queue.size() > 1){
            queue.poll();
            queue.add(queue.poll());
        }
        System.out.println(queue.poll());

        br.close();
    }
}

'Coding Test > 백준' 카테고리의 다른 글

[백준/자바] 10989 - 수 정렬하기 3  (0) 2023.05.12
[백준/자바] 10798 - 세로읽기  (0) 2023.05.12
[백준/자바] 2566 - 최댓값  (0) 2023.05.11
[백준/자바] 9012 - 괄호  (0) 2023.05.11
[백준/자바] 2738 - 행렬 덧셈  (0) 2023.05.11