순서를 바꿔주면 되는 간단(?)한 문제다. 학교에서 1학년 때 접해본듯...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
public class Main {
static Integer[] dp;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s = bf.readLine();
String[] split = s.split(" ");
int num = Integer.parseInt(split[0]);
int num2 = Integer.parseInt(split[1]);
int[] array = new int[num];
for (int i = 0; i < num; i++) {
array[i] = i + 1;
}
int temp = 0;
for (int i = 0; i < num2; i++) {
String second = bf.readLine();
String[] number = second.split(" ");
int N = Integer.parseInt(number[0]);
int M = Integer.parseInt(number[1]);
temp = array[N-1];
array[N-1] = array[M-1];
array[M-1] = temp;
}
for(Integer result : array){
System.out.print(result + " ");
}
}
}
'PS > 백준' 카테고리의 다른 글
백준 - 세 수 ( Java ) (0) | 2023.11.25 |
---|---|
백준 - 별찍기3 ( Java ) (1) | 2023.11.23 |
백준 - 01타일 ( Java ) (0) | 2023.11.16 |
백준 - 학점 계산 (0) | 2023.11.14 |
백준 - 1로 만들기 (0) | 2023.11.13 |
댓글