충분히 쌓여가는
백준[Java] 10813번 공 바꾸기 본문
https://www.acmicpc.net/problem/10813
문제풀이 1
import java.util.Scanner;
public class _10813_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = i+1;
}
for (int i = 0; i < M; i++) {
int tmp;
int F = sc.nextInt();
int E = sc.nextInt();
tmp = arr[F-1];
arr[F-1] = arr[E-1];
arr[E-1] = tmp;
}
sc.close();
for (int baguni : arr)
System.out.print(baguni + " ");
}
}
문제풀이 2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class _10813_2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = i+1;
}
for (int i = 0; i < M; i++) {
int tmp;
st = new StringTokenizer(br.readLine(), " ");
int F = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
tmp = arr[F-1];
arr[F-1] = arr[E-1];
arr[E-1] = tmp;
}
br.close();
for (int baguni : arr)
System.out.print(baguni + " ");
}
}
문제풀이 3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class _10813_3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = i+1;
}
for (int i = 0; i < M; i++) {
int tmp;
st = new StringTokenizer(br.readLine(), " ");
int F = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
tmp = arr[F-1];
arr[F-1] = arr[E-1];
arr[E-1] = tmp;
}
br.close();
for (int baguni : arr)
sb.append(baguni).append(" ");
System.out.println(sb);
}
}
'IT > Java[백준]' 카테고리의 다른 글
백준[Java] 3052번 나머지 (HashSet 사용) (0) | 2023.03.01 |
---|---|
백준[Java] 5597번 과제 안 내신 분..? (0) | 2023.02.28 |
백준[Java] 10810번 공 넣기 (0) | 2023.02.28 |
백준[Java] 2562번 최댓값 (0) | 2023.02.27 |
백준[Java] 10871번 최소,최대 (0) | 2023.02.27 |