JAVA-BAEKJOON/4단계 1차원 배열

[백준/04-06] 10813 공 바꾸기

써머레인 2024. 3. 17. 20:30

NO.10813 

 

풀이코드

import java.util.Scanner;

public class Main{

	public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int arr[] = new int[N];
        int M = sc.nextInt();
        int temp;

        for(int i = 0; i < arr.length; i++) {
            arr[i] = i + 1; 
        }  
        for(int j = 0; j < M; j++) {
            int I = sc.nextInt();
            int J = sc.nextInt();

            temp = arr[I-1];
            arr[I-1] = arr[J-1];
            arr[J-1] = temp;
        }
        for(int k = 0; k <arr.length; k++) {
            System.out.print(arr[k] + " ");
        }
	}
}

 

📌 i 번 바구니와 j 번 바구니에 들어있는 공을 서로 교환해야한다.
      두개의 값이 들어올 때마다 배열에 있는 값들을 교환시켜보자.