Algorithm, Problem Solving/백준(boj)

[백준][Java] 11292 - 키 큰 사람

태오님 2023. 3. 24.

https://www.acmicpc.net/problem/11292

 

11292번: 키 큰 사람

민우는 학창시절 승부욕이 강해서 달리기를 할 때에도 누가 가장 빠른지를 중요하게 생각하고, 시험을 볼 때에도 누가 가장 성적이 높은지를 중요하게 생각한다. 이번에 반에서 키를 측정하였

www.acmicpc.net

난이도 : S5

유형 : 자료구조, 정렬


문제 풀이

키를 기준으로 내림차 정렬을 해준 다음 제일 큰 키 값과 동일한 사람들을 차례대로 출력하면 된다.


코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    public class Person implements Comparable<Person> {
        String name;
        double height;

        public Person(String name, double height) {
            this.name = name;
            this.height = height;
        }

        @Override
        public int compareTo(Person o) {
            if (o.height - this.height > 0) {
                return 1;
            } else if (o.height == this.height) {
                return 0;
            } else {
                return -1;
            }
        }
    }

    private int N;
    private Person[] people;

    public void solution() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;

        while ((N = Integer.parseInt(br.readLine())) != 0) {
            people = new Person[N];

            for (int i = 0; i < N; i++) {
                st = new StringTokenizer(br.readLine());
                people[i] = new Person(st.nextToken(), Double.parseDouble(st.nextToken()));
            }

            Arrays.sort(people);

            sb.append(people[0].name).append(" ");
            for (int i = 1; i < N; i++) {
                if (people[0].height != people[i].height) {
                    break;
                }
                sb.append(people[i].name).append(" ");
            }
            sb.append('\n');
        }

        System.out.println(sb);
    }

    public static void main(String[] args) throws IOException {
        new Main().solution();
    }
}

댓글