ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 7568번 - 덩치
    알고리즘/백준 2023. 6. 4. 15:44
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    
    public class Main {
    	
    	static class Person {
    		int weight;
    		int height;
    		int count;
    		
    		Person(int weight, int height) {
    			this.weight = weight;
    			this.height = height;
    			this.count = 1;
    		}
    		
    		void countUp() {
    			this.count++;
    		}
    	}
    	
    	public static void main(String[] args) throws IOException {
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		StringTokenizer st;
    		int N = Integer.parseInt(br.readLine());
    		Person[] persons = new Person[N];
    		Person person, tmp;
    		for(int i=0;i<N;i++) {
    			st = new StringTokenizer(br.readLine());
    			persons[i] = new Person(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
    		}
    		for(int i=0;i<N;i++) {
    			person = persons[i];
    			for(int j=0;j<N;j++) {
    				tmp = persons[j];
    				if(person.weight < tmp.weight && person.height < tmp.height) {
    					person.countUp();
    				}
    			}
    		}
    		for(int i=0;i<N;i++) {
    			System.out.print(persons[i].count+" ");
    		}
    		br.close();
    	}
    }

    어려워보이기 쉬우나 난이도를 확인했다면 브루트 포스로 해결해야 하는 것을 느낄 수 있습니다.

    (만약 더 효율적으로 해야 한다면 꽤 어려운 문제가 될 것 같네요.)

     

    각 사람마다 몸무게, 키와 등수가 존재하기 때문에 관리하기 용이하도록 하나의 class로 사람을 선언하여 관리했습니다.

     

    모든 사람의 정보를 입력하고 배열에 넣은 후, 각 사람마다 배열의 모든 원소에서 덩치가 큰 사람의 수를 세어 문제를 해결할 수 있습니다. 문제의 조건에 따라 더 큰 사람이 있을 때마다 count를 올려주고

     

    끝나면 입력받은 순서대로 count를 출력하면 완료됩니다.

    '알고리즘 > 백준' 카테고리의 다른 글

    10773번 - 제로  (0) 2023.06.06
    9012번 - 괄호  (0) 2023.06.04
    4949번 - 균형잡힌 세상  (0) 2023.06.04
    2805번 - 나무 자르기  (0) 2023.06.04
    15829번 - Hashing  (0) 2023.03.19
Designed by Tistory.