https://www.acmicpc.net/problem/11650
comparator
return 값이 음수이면 위치를 바꾸지 않음
return 값이 양수이면 위치를 바꿈
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class baek11650 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int input = Integer.parseInt(br.readLine());
int [][] array = new int[input][2];
for(int i=0;i<input;i++){
StringTokenizer st = new StringTokenizer(br.readLine());
array[i][0] = Integer.parseInt(st.nextToken());
array[i][1] = Integer.parseInt(st.nextToken());
}
Arrays.sort(array, (arr_1 , arr_2)->{
if(arr_1[0] == arr_2[0]){
return arr_1[1] - arr_2[1];
}
else{
return arr_1[0] - arr_2[0];
}
});
for(int i=0;i<input;i++){
System.out.println(array[i][0]+" "+array[i][1]);
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class baek11650 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int input = Integer.parseInt(br.readLine());
int [][] array = new int[input][2];
for(int i=0;i<input;i++){
StringTokenizer st = new StringTokenizer(br.readLine());
array[i][0] = Integer.parseInt(st.nextToken());
array[i][1] = Integer.parseInt(st.nextToken());
}
Arrays.sort(array, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0]) {
return o1[1] - o2[1];
}
else{
return o1[0]-o2[0];
}
}
});
for(int i=0;i<input;i++){
System.out.println(array[i][0]+" "+array[i][1]);
}
}
}
'Coding Test > Java' 카테고리의 다른 글
[배열] 타노스 배열 (0) | 2023.07.13 |
---|---|
[배열] 스파이 찾기 (1) | 2023.07.13 |