BackJoon Algorithm - Java
문제
풀이
- 2차원 배열은 Arrays.sort() 메서드를 사용할수없으므로 람다식을 통해 사용한다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Back_11651 {
public static void main(String[] args) throws IOException {
// given
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int arr[][] = new int[N][2];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
arr[i][1] = Integer.parseInt(st.nextToken());
arr[i][0] = Integer.parseInt(st.nextToken());
}
// when
Arrays.sort(arr, (e1, e2) -> {
if (e1[0] == e2[0]) {
return e1[1] - e2[1];
} else {
return e1[0] - e2[0];
}
});
// then
for (int i = 0; i < N; i++) {
System.out.println(arr[i][1] + " " + arr[i][0]);
}
br.close();
}
}
댓글남기기