JAVA/Coding Test

[JAVA] 백준 7568 덩치_브루트포스

오늘도개발 2023. 2. 28. 15:29

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

 

7568번: 덩치

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

www.acmicpc.net

 

접근 :

 

- 입력 받은 n 명의 사람들의 정보를 받는다.

 

- 모든 정보를 받은 후, 1명씩 선정하여 나머지 사람들에 대해서 키와 몸무게가 큰 사람이 있는지 확인한다.

 

- 최종 덩치 등수는 자신보다 키와 몸무게가 큰 사람들의 수 +1로 표현된다.

 

코드 구현:  

 

import java.io.*;

public class Main {

	// 덩치로 표현된 사람 객체 정의
	public static class Person {

		private int rank;
		private int weight;
		private int height;

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

		public void setRank(int rank) {
			this.rank = rank;
		}

		public int getWeight() {
			return this.weight;
		}

		public int getHeight() {
			return this.height;
		}

		public int getRank() {
			return this.rank;
		}

	}

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());

		String[] inputs = new String[2];
		Person[] person_array = new Person[n];
		StringBuilder sb = new StringBuilder();
		int count = 0;

		// n 회 만큼 정보를 받아서 사람 객체를 생성
		for (int i = 0; i < n; i++) {
			inputs = br.readLine().split(" ");
            
            		// Person(int weight, int height) 
			person_array[i] = new Person(Integer.parseInt(inputs[0]), Integer.parseInt(inputs[1]));
		}

		// 한명씩 선정하여 보다 큰 사람수 가 몇 명인지 확인
		for (int j = 0; j < n; j++) {
			count = 0;

			// 모든 사람과 비교
			for (int k = 0; k < n; k++) {

				// 키와 몸무게 모두 크면 카운트 증가
				if (person_array[j].getWeight() < person_array[k].getWeight()
						&& person_array[j].getHeight() < person_array[k].getHeight()) {
					count++;
				}
			}
			// count + 1 로 최종 등수 입력
			person_array[j].setRank(count + 1);

			// 화면 출력을 위한 문자열
			sb.append(count + 1);
			sb.append(" ");
		}
		// 결과 화면출력
		System.out.println(sb);
		br.close();
	}
}