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 번 바구니에 들어있는 공을 서로 교환해야한다.
두개의 값이 들어올 때마다 배열에 있는 값들을 교환시켜보자.
'JAVA-BAEKJOON > 4단계 1차원 배열' 카테고리의 다른 글
[백준/04-05] 10810 공 넣기 (0) | 2024.03.10 |
---|---|
[백준/04-04] 2562 최댓값 (0) | 2024.03.10 |
[백준/04-03] 10818 최소, 최대 (0) | 2024.03.05 |
[백준/04-02] 10871 X보다 작은 수 (0) | 2024.03.01 |
[백준/04-01] 10807 개수 세기 (0) | 2024.03.01 |