BOJ 10804. 카드 역배치

less than 1 minute read

BOJ 3085. 카드 역배치

image image image image

풀이

1~20까지 숫자를 오름차순으로 저장하고, start와 end를 입력받아 start와 end사이를 서로 바꾸어준다.

에를들어 5~9 사이의 수를 바꾼다고 하였을 때
5, 6, 7, 8, 9 -> 9, 6, 7, 8, 5 -> 9, 8, 7, 6, 5 이 된다.

class Main {
    static int[] arr = new int[21];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i < arr.length; i++) {
            arr[i] = i;
        }
        for (int i = 1; i <= 10; i++) {
            int start = sc.nextInt();
            int end = sc.nextInt();
            swap(start, end);
        }

        for (int i = 1; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    static void swap(int start, int end) {
        for (int i = 0; i <= (end - start) / 2; i++) {
            int temp = arr[start + i];
            arr[start + i] = arr[end - i];
            arr[end - i] = temp;
        }
    }
}