# BOJ07568 덩치

https://www.acmicpc.net/problem/7568 (opens new window)

A, B 두 사람이 존재한다. 두 사람의 덩치가 각각 (56, 177), (45, 165) 이면 A는 B보다 몸무게와 키가 모두 크기 때문에 더큰 덩치를 갖고 있다고 판단한다. 만약 C, D 두상의 덩치가 각각 (45, 181), (55, 173) 이라면 몸무게는 D가 더 크고, 키는 C가 더 크기 때문에 C와 D는 상대적으로 더 큰 덩치를 가지고 있다고 판단할 수 없다.

# Person.java

먼저 사람 표현을 위한 Person 클래스이다.

static class Person {
    private final int weight;
    private final int height;
    private int rank;

    public Person(int weight, int height, int rank) {
        this.weight = weight;
        this.height = height;
        this.rank = rank;
    }
}

rank는 순위 표현을 위한 필드이다.

# 제출 코드

먼저 사람들의 키와 몸무게를 입력받아 저장한다.

for (int i = 0; i < n; i++) {
    String[] personInfo = scanner.nextLine().split(" ");
    persons.add(new Person(Integer.parseInt(personInfo[0]), Integer.parseInt(personInfo[1]), 1));
}

이제 두 사람의 몸무게와 키를 비교하여 더 큰 사람이 존재하면 자신의 rank를 증가시킨다.

for (Person personA : persons) {
    for (Person personB : persons) {
        if (!personA.equals(personB)) {
            if (personA.height < personB.height && personA.weight < personB.weight) {
                personA.rank++;
            }
        }
    }
}

아래는 최종 제출 코드이다.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BOJ7568 {

    static class Person {
        private final int weight;
        private final 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) {
        Scanner scanner = new Scanner(System.in);

        List<Person> persons = new ArrayList<>();
        int n = scanner.nextInt();
        scanner.nextLine();

        for (int i = 0; i < n; i++) {
            String[] personInfo = scanner.nextLine().split(" ");
            persons.add(new Person(Integer.parseInt(personInfo[0]), Integer.parseInt(personInfo[1]), 1));
        }

        for (Person personA : persons) {
            for (Person personB : persons) {
                if (!personA.equals(personB)) {
                    if (personA.height < personB.height && personA.weight < personB.weight) {
                        personA.rank++;
                    }
                }
            }
        }

        persons.forEach(person -> System.out.print(person.rank + " "));
    }
}
#algorithm #BOJ #구현 #브루트포스 알고리즘
last updated: 10/10/2021, 6:09:25 PM