๐ŸŒท๐ŸŒผ๋ชจ์—ฌ๋ด์š” ๊ฐœ๋ฐœ์˜์ˆฒ๐ŸŒท๐ŸŒผ

[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);
		}
		
    }

}
Comments