ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Baekjoon Online Judge] 7568번: 덩치
    문제 풀이/Baekjoon Online Judge 2021. 3. 17. 12:43
     

    7568번: 덩치

    우리는 사람의 덩치를 키와 몸무게, 이 두 개의 값으로 표현하여 그 등수를 매겨보려고 한다. 어떤 사람의 몸무게가 x kg이고 키가 y cm라면 이 사람의 덩치는 (x, y)로 표시된다. 두 사람 A 와 B의 덩

    www.acmicpc.net

    요구사항

     - 우리는 사람의 덩치를 키와 몸무게, 이 두 개의 값으로 표현하여 그 등수를 매겨보려고 한다. 어떤 사람의 몸무게가 x kg이고 키가 y cm라면 이 사람의 덩치는 (x, y)로 표시된다.

     - 두 사람 A 와 B의 덩치가 각각 (x, y), (p, q)라고 할 때 x > p 그리고 y > q 이라면 우리는 A의 덩치가 B의 덩치보다 "더 크다"고 말한다.

     - N명의 집단에서 각 사람의 덩치 등수는 자신보다 더 "큰 덩치"의 사람의 수로 정해진다.

     - 만일 자신보다 더 큰 덩치의 사람이 k명이라면 그 사람의 덩치 등수는 k+1이 된다. 이렇게 등수를 결정하면 같은 덩치 등수를 가진 사람은 여러 명도 가능하다. 

    입력

     - 첫 줄에는 전체 사람의 수 N이 주어진다. 그리고 이어지는 N개의 줄에는 각 사람의 몸무게와 키를 나타내는 양의 정수 x와 y가 하나의 공백을 두고 각각 나타난다.

    출력

     - 여러분은 입력에 나열된 사람의 덩치 등수를 구해서 그 순서대로 첫 줄에 출력해야 한다. 단, 각 덩치 등수는 공백문자로 분리되어야 한다.


    몸무게와 키 그리고 등수 표현을 위한 클래스를 선언하였다.

     

    public static class Person {
        private int weight; // 몸무게
        private int height; // 키
        private int rank; // 등수
    
        public Person(int weight, int height, int rank) {
            this.weight = weight;
            this.height = height;
            this.rank = rank;
        }
    }

     

    입력으로 몸무게와 키가 주어진다. 초기의 등수는 1로 설정하여 값을 입력받는다.

     

    int n = Integer.parseInt(bufferedReader.readLine());
    List<Person> persons = new ArrayList<>();
    
    for (int i = 0; i < n; i++) {
        String[] input = bufferedReader.readLine().split(" ");
        int weight = Integer.parseInt(input[0]);
        int height = Integer.parseInt(input[1]);
        persons.add(new Person(weight, height, 1));
    }

     

    자신을 제외한 각각의 항목을 조건에 맞추어 ((x, y), (p, q)라고 할 때 x > p 그리고 y > q) 비교한다. 조건에 만족하면 rank를 증가시켜 등수를 설정한다.

     

    for (Person person1 : persons) {
        for (Person person2 : persons) {
            if (person1.weight < person2.weight && person1.height < person2.height)
                person1.rank += 1;
        }
    }

     

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Baekjoon7568 {
    
        public static class Person {
            private int weight; // 몸무게
            private int height; // 키
            private int rank; // 등수
    
            public Person(int weight, int height, int rank) {
                this.weight = weight;
                this.height = height;
                this.rank = rank;
            }
        }
    
        public static void main(String[] args) throws IOException {
    
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
    
            int n = Integer.parseInt(bufferedReader.readLine());
            List<Person> persons = new ArrayList<>();
    
            for (int i = 0; i < n; i++) {
                String[] input = bufferedReader.readLine().split(" ");
                int weight = Integer.parseInt(input[0]);
                int height = Integer.parseInt(input[1]);
                persons.add(new Person(weight, height, 1));
            }
    
            for (Person person1 : persons) {
                for (Person person2 : persons) {
                    if (person1.weight < person2.weight && person1.height < person2.height)
                        person1.rank += 1;
                }
            }
    
            for (Person person : persons)
                System.out.print(person.rank + " ");
    
            bufferedReader.close();
        }
    }

    댓글

Designed by Tistory.