PS/BaekJoon
11650 JAVA ์ขํ ์ ๋ ฌํ๊ธฐ
chaerlo127
2022. 7. 12. 22:48
728x90
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 Main {
public static void main(String[] args) {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int a;
try {
a = Integer.parseInt(buffer.readLine());
int[][] xy = new int[a][2];
for(int i = 0; i<a; i++) {
StringTokenizer st = new StringTokenizer(buffer.readLine()); // ๊ณต๋ฐฑ ๋จ์๋ก ์
๋ ฅ ๋ฐ์
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
xy[i][0] = x;
xy[i][1] = y;
}
Arrays.sort(xy, new Comparator<int []>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] == o2[0]) { // ์ฒซ ๋ฒ์งธ ์์๋ค์ด ๊ฐ๋ค๋ฉด
return o1[1] - o2[1]; // ๋ ๋ฒ์งธ ๋ฐฐ์ด ์ค๋ฆ์ฐจ์
}
return o1[0] - o2[0]; // ์ฒซ ๋ฒ์งธ ์์ ์ค๋ฆ์ฐจ์
}
});
for(int i = 0; i<a; i++) {
sb.append(xy[i][0]).append(" ").append(xy[i][1]).append("\n");
}
System.out.println(sb);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
โจ StringTokenizer
๋ฌธ์์ด์ ์ฐ๋ฆฌ๊ฐ ์ง์ ํ ๊ตฌ๋ถ์๋ก ๋ฌธ์์ด์ ์ชผ๊ฐ์ฃผ๋ ํด๋์ค
[์ถ์ฒ ๋ฐ ์ค์ ๋ฌธ๋ฒ] https://jhnyang.tistory.com/398
โจ Comparator
[์ถ์ฒ ๋ฐ ์ค์ ๋ฌธ๋ฒ] https://velog.io/@neity16/Java-%EA%B8%B0%EB%B3%B83-Comparable-Comparator-%EC%B0%A8%EC%9D%B4%EC%A0%90#%EC%84%A4%EB%AA%85-1
728x90