๐ท๐ผ๋ชจ์ฌ๋ด์ ๊ฐ๋ฐ์์ฒ๐ท๐ผ
[JAVA/์ฝ๋ฉํ ์คํธ] ์ขํ ์ ๋ ฌ ๋ณธ๋ฌธ
๊ฐ๋ฐ/์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด JAVA
[JAVA/์ฝ๋ฉํ ์คํธ] ์ขํ ์ ๋ ฌ
์์ผ์ด 2021. 6. 27. 17:57๋ฐ์ํ
์ค๋ช
N๊ฐ์ ํ๋ฉด์์ ์ขํ(x, y)๊ฐ ์ฃผ์ด์ง๋ฉด ๋ชจ๋ ์ขํ๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์ธ์.
์ ๋ ฌ๊ธฐ์ค์ ๋จผ์ x๊ฐ์ ์ํด์ ์ ๋ ฌํ๊ณ , x๊ฐ์ด ๊ฐ์ ๊ฒฝ์ฐ y๊ฐ์ ์ํด ์ ๋ ฌํฉ๋๋ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ์ขํ์ ๊ฐ์์ธ N(3<=N<=100,000)์ด ์ฃผ์ด์ง๋๋ค.
๋ ๋ฒ์งธ ์ค๋ถํฐ N๊ฐ์ ์ขํ๊ฐ x, y ์์ผ๋ก ์ฃผ์ด์ง๋๋ค. x, y๊ฐ์ ์์๋ง ์ ๋ ฅ๋ฉ๋๋ค.
์ถ๋ ฅ
N๊ฐ์ ์ขํ๋ฅผ ์ ๋ ฌํ์ฌ ์ถ๋ ฅํ์ธ์.
์์ ์ ๋ ฅ 1
5
2 7
1 3
1 2
2 5
3 6
์์ ์ถ๋ ฅ 1
1 2
1 3
2 5
2 7
3 6
import java.util.*;
public class Main {
public static void solution(int n, int[][] array){
int[][] tmp = new int[1][2];
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(array[j][0] < array[i][0]){
tmp[0] = array[j];
array[j] = array[i];
array[i] = tmp[0];
}else if(array[i][0] == array[j][0]){
if(array[j][1] < array[i][1]){
tmp[0] = array[j];
array[j] = array[i];
array[i] = tmp[0];
}
}
}
System.out.println(array[i][0]+" " + array[i][1]);
}
}
public static void main(String[] args) {
Main main = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int[][] array = new int[n][2];
for(int i=0; i<n; i++){
for(int j=0; j<2; j++){
array[i][j] = kb.nextInt();
}
}
Main.solution(n, array);
}
}
import java.util.*;
class Point implements Comparable<Point>{
public int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Point o) {
if(this.x==o.x) {
// ์ค๋ฆ์ฐจ์
return this.y - o.y;
// ๋ด๋ฆผ์ฐจ์
//return o.y - this.y;
}else {
// ์ค๋ฆ์ฐจ์
return this.x - o.x;
// ๋ด๋ฆผ์ฐจ์
//return o.x - this.x;
}
}
}
class Main {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
ArrayList<Point> arr = new ArrayList<>();
for(int i=0; i<n; i++) {
int x = kb.nextInt();
int y = kb.nextInt();
arr.add(new Point(x, y));
}
Collections.sort(arr);
for(Point o : arr) {
System.out.println(o.x + " " + o.y);
}
}
}
'๊ฐ๋ฐ > ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA/์ฝ๋ฉํ ์คํธ] ๋ฎค์ง๋น๋์ค(๊ฒฐ์ ์๊ณ ๋ฆฌ์ฆ) (0) | 2021.06.28 |
---|---|
[JAVA/์ฝ๋ฉํ ์คํธ] ์ด๋ถ๊ฒ์ (0) | 2021.06.27 |
[JAVA/์ฝ๋ฉํ ์คํธ] ์ฅ๋๊พธ๋ฌ๊ธฐ (0) | 2021.06.27 |
[JAVA/์ฝ๋ฉํ ์คํธ] ์ค๋ณต ํ์ธ (0) | 2021.06.27 |
[JAVA/์ฝ๋ฉํ ์คํธ] ์ฝ์ ์ ๋ ฌ (0) | 2021.06.18 |
Comments