코테/코드트리

[코드트리/NOVICE MID] 줄 세우기, 줄 세우기 2 (JAVA)

imname1am 2023. 10. 31. 20:02
반응형

🔺 문제

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

 

 

🔺 코드

- 줄 세우기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
import java.io.*;
 
class Person implements Comparable<Person> {
    int h, w, idx;
 
    public Person(int h, int w, int idx) {
        this.h = h;
        this.w = w;
        this.idx = idx;
    }
 
    @Override
    public int compareTo(Person p) {
        if(this.h != p.h) { // 조건1) 키가 더 큰 학생이 앞에 와야
            return p.h - this.h;
        }
        else {
            if(this.w != p.w) 
                return p.w - this.w;    // 조건2) 키가 동일하다면, 몸무게가 더 큰 학생이 앞에 와야
            return this.idx - p.idx;    // 조건3) 키와 몸무게가 동일하다면, 번호가 작은 학생이 앞에 와야
        }
    }
}
 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        Person[] people = new Person[n];
        for(int i = 0 ; i < n ; i++) {
            String[] str = br.readLine().split(" ");
            people[i] = new Person(Integer.parseInt(str[0]), Integer.parseInt(str[1]), i + 1);
        }
        
        Arrays.sort(people);
 
        for(Person p : people) {
            System.out.println(p.h + " " + p.w + " " + p.idx);
        }
    }
}
cs

🧩  해결 아이디어

• 사용자 정의 정렬

- 키가 다르다면, 키가 큰 순으로 정렬함 (= 내림차순)

- 키가 동일하다면, 몸무게가 더 큰 학생이 앞에 와야 함 (= 내림차순)

- 키와 몸무게가 동일하다면, 번호가 작은 학생이 앞에 와야 함 (= 내림차순)

 

 

다른 정답 코드 확인하기 (toString 활용)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.*;
import java.util.*;
 
public class Main {
    static int N;
    static Person[] persons;
 
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
 
        N = Integer.parseInt(br.readLine());
        persons = new Person[N];
        for(int i=0;i<N;i++){
            st = new StringTokenizer(br.readLine());
            int height = Integer.parseInt(st.nextToken());
            int weight = Integer.parseInt(st.nextToken());
 
            Person s = new Person(height, weight, i+1);
            persons[i] = s;
        }
 
        Arrays.sort(persons);
        for(int i = 0 ; i < N ; i++){
            System.out.println(persons[i]);
        }
    }
}
 
class Person implements Comparable<Person> {
    int height;
    int weight;
    int number;
 
    Person(){}
    public Person(int height, int weight, int number){
        this.height = height;
        this.weight = weight;
        this.number = number;
    }
 
    @Override
    public String toString(){
        return height + " " + weight + " " + number;
    }
    
    @Override
    public int compareTo(Person o1){
        if(height == o1.height){
            if(weight == o1.weight)
                return number - o1.number;
            return o1.weight - weight;
        }
        else return o1.height - height;
    }
}
cs

 

 

 

 

- 줄 세우기2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.*;
import java.io.*;
 
class Person implements Comparable<Person> {
    int h, w, idx;
 
    public Person(int h, int w, int idx) {
        this.h = h;
        this.w = w;
        this.idx = idx;
    }
 
    @Override
    public int compareTo(Person p) {
        if(this.h != p.h) { // 키가 더 작은 학생이 앞에 와야 함
            return this.h - p.h;
        }
        return p.w - this.w;    // 키가 동일하다면, 몸무게가 더 큰 학생이 앞에 와야
    }
}
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        
        Person[] people = new Person[n];
        for(int i = 0 ; i < n ; i++) {
            String[] str = br.readLine().split(" ");
            people[i] = new Person(Integer.parseInt(str[0]), Integer.parseInt(str[1]), i + 1);
        }
        
        Arrays.sort(people);
 
        // 출력
        for(Person p : people) {
            System.out.println(p.h + " " + p.w + " " + p.idx);
        }
    }
}
cs

 

🧩  해결 아이디어

• 사용자 정의 정렬

- 키가 다르다면, 키가 작은 순(=오름차순)으로 정렬함

- 키가 동일하다면, 몸무게가 더 큰 학생(=내림차순)이 앞에 와야 함

 


💬 느낀 점

스겜스겜!!

 

1회독 2회독 3회독 4회독 5회독
V        
반응형