7. 좌표 정렬
설명
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
#정렬의 방법
1. 직접 정렬방법을 지정해주는 방법
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
arr[i][j] = in.nextInt();
}
}
for (Point o : solution(n, arr)) {
System.out.println(o.x + " " + o.y);
}
}
static ArrayList<Point> solution(int n, int[][] arr) {
ArrayList<Point> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int x = arr[i][0];
int y = arr[i][1];
list.add(new Point(x, y));
}
Collections.sort(list, new Comparator<Point>() {
public int compare(Point o1, Point o2) {
if (o1.x == o2.x)
return o1.y - o2.y;
else
return o1.x - o2.x;
}
});
return list;
}
}
2. 클래스에서 상속을 이용하는 방법
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Point implements Comparable<Point> {
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;
else
return this.x - o.x;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
arr[i][j] = in.nextInt();
}
}
for (Point o : solution(n, arr)) {
System.out.println(o.x + " " + o.y);
}
}
static ArrayList<Point> solution(int n, int[][] arr) {
ArrayList<Point> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int x = arr[i][0];
int y = arr[i][1];
list.add(new Point(x, y));
}
Collections.sort(list);
return list;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
//좌표 정렬
//모든 좌표를 오름차순으로 정렬하는 프로그램 작성
//정렬 기준 : x값에 의해 정렬 후, x값이 같으면 y값에 의해 정렬
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;
//오름차순
// else
// return this.x-o.x;
//오름차순
return this.x==o.x?this.y-o.y:this.x-o.x;
//x가 같으면, y가 작은 순, 아니면 x순
}
}
public class Main {
public static void main(String[] args) {
Main T =new Main();
Scanner kb=new Scanner(System.in);
int n=kb.nextInt();
ArrayList<Point> arr = new ArrayList<>();
//점을 나타내는 point자료형
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);
}
}
# 람다식 이용
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int[][] arrs = new int[n][2];
for(int i=0;i<n;i++){
int x=in.nextInt();
int y=in.nextInt();
int[] arr = new int[2];
arrs[i]=new int[]{x,y};
}
Arrays.sort(arrs, (o1, o2) -> o1[0]==o2[0]?o1[1]-o2[1]: o1[0]-o2[0]);
for(int[] arr : arrs) System.out.println(arr[0]+" "+arr[1]);
}
}'Java > Java 알고리즘 인프런' 카테고리의 다른 글
| stream 메서드를 이용해 간단히 값 구하기 (0) | 2022.06.05 |
|---|---|
| [Ch.06 - SortSearch] 08. 이분검색 ## (0) | 2022.06.05 |
| [Ch.06 - SortSearch] 06. 장난 꾸러기 (+ arr.clone() ) (0) | 2022.06.05 |
| [Ch.06 - SortSearch] 05. 중복 확인 (+ Set, for-while) (0) | 2022.06.05 |
| [Ch.06 - SortSearch] 04. LRU (Least Recently Used) 최근 최소 사용 알고리즘 ## (+ list.set, list.remove) (0) | 2022.06.05 |