코딩 테스트/알고리즘
[백준] 24053 알고리즘 수업(Gold.5) - 삽입 정렬 3
토자맨
2024. 7. 8. 15:24
문제
https://www.acmicpc.net/problem/24053
코드
로직은 정말 간단하다. 삽입 의사 정렬 코드를 구현하고 숫자가 바뀔 때마다 배열 a, b가 같은지 확인한 후 같다면 1을 출력하고 같은 경우가 한 번도 없다면 0을 출력하면 된다.
이게 골드5 문제가 맞나 ..?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class _24053 {
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st1 = new StringTokenizer(br.readLine());
StringTokenizer st2 = new StringTokenizer(br.readLine());
int[] a = new int[n];
int[] b = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st1.nextToken());
b[i] = Integer.parseInt(st2.nextToken());
}
insert_sort(a, b);
}
public static void insert_sort(int[] a, int[] b) {
if (Arrays.equals(a, b)) {
System.out.println(1);
return;
}
for (int i = 1; i < a.length; i++) {
int loc = i-1;
int tmp = a[i];
while (loc >= 0 && a[loc] > tmp) {
a[loc+1] = a[loc];
loc--;
if (Arrays.equals(a, b)) {
System.out.println(1);
return;
}
}
if (loc + 1 != i) {
a[loc + 1] = tmp;
if (Arrays.equals(a, b)) {
System.out.println(1);
return;
}
}
}
System.out.println(0);
}
}