백준7568 - 덩치

문제

접근

  1. x,y p,q 가 있다고 할 때
  2. x,y가 p,q 보다 둘다 커야 한다 (&& 사용)
  3. 해당 인물의 등수는 자신보다 큰 사람 k + 1
  4. N은 50으로 작음. <- O(N^2) 도 문제가 없다!
  • 아 브루트포스!

구현 전

  1. 굳이 class 를 만들어서 접근할까?
  2. 2차원 배열 쓸까?

어차피 하나하나 집어넣을 것, class를 써서 익숙해지자는 마인드

StringBuilder에 현재 인물의 등수와, 공백을 통해 출력!

구현

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

public class Main{
  public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
      
    int N = Integer.parseInt(br.readLine());
    
    People[] people = new People[N];
    
    for (int i = 0; i < N; i++) {
      st = new StringTokenizer(br.readLine());
      int x = Integer.parseInt(st.nextToken());
      int y = Integer.parseInt(st.nextToken());
      
      people[i] = new People(x,y);
    }
    br.close();
    
    StringBuilder sb = new StringBuilder();
    
    for (int i = 0; i < N; i++) {
      People curPeople = people[i];
      int biggerCnt = 0;
      
      for(int j = 0; j < N; j++) {
        
        if (j == i) continue;
        People targetPeople = people[j];
        
        if (curPeople.x < targetPeople.x && curPeople.y < targetPeople.y) {
          biggerCnt++;
        } 
        
      }  
      sb.append(biggerCnt + 1);
      sb.append(' ');
    }
    System.out.println(sb);
  }
  static class People {
    int x;
    int y; 
    
    public People(int x, int y) {
      this.x = x;
      this.y = y;
    }
  }
}

결론

  • class 만드는 방법에 대해 더 학습할 필요가 있음.
    • getter / setter 외 오버라이딩 등 이것저것!
메모리: 14156 KB, 시간: 116 ms

나쁘지 않구료.

근데 vscode로 자바 쓰는건 좀 별로인 것 같다. 흐으으음.