문제 보기

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

 

 

풀이

입력받은 국가의 순위만 알면 되기 때문에 전체를 정렬할 필요는 없다.

순위가 더 높은 국가가 얼마나 있는지만 세어보면 됩니다.

 

1. 2차원 배열에 국가별 매달 수 저장

int KIndex = 0;
int[][] nation = new int[N][4];

for (int i = 0; i < N; i++) {
  input = cs.nextLine().split(" ");
  for (int j = 0; j < 4; j++) {
    nation[i][j] = Integer.parseInt(input[j]);
  }
  if(nation[i][0] == K) {
    KIndex = i;
  }
}

 

우선 나는 2차원 배열을 이용해 국가들의 메달 수를 저장했다.

그리고 해당하는 국가의 인덱스도 따로 저장해 놨다.

 

2. 높은 등수의 국가 count

int count = 0;

for(int i = 0; i < N; i++) {
  if(KIndex == i) {
    continue;
  }
  for(int j = 1; j < 4; j++) {
    if(nation[i][j] > nation[KIndex][j]) {
      count++;
      break;
    } else if(nation[i][j] < nation[KIndex][j]) {
      break;
    }
  }
}

 

해당 국가를 제외한 다른 국가들을 순회한다.

입력을 그대로 2차원 배열에 저장해 놓았기 때문에 내부 for문을 통해 금->은->동 순으로 메달 수를 비교하게 된다.

금메달의 수가 더 많으면 count++ 하고 break

적으면 그냥 break

같을 경우에는 j의 수가 더 커지며 은메달로 넘어가게 됩니다.

이렇게 동메달까지 순위를 비교합니다.

 

전체 코드

import java.util.Scanner;

class Main {

  public static void main(String[] args) {

    Scanner cs = new Scanner(System.in);

    String[] input = cs.nextLine().split(" ");
    int N = Integer.parseInt(input[0]);
    int K = Integer.parseInt(input[1]);

    int KIndex = 0;
    int[][] nation = new int[N][4];
    int count = 0;
    for (int i = 0; i < N; i++) {
      input = cs.nextLine().split(" ");
      for (int j = 0; j < 4; j++) {
        nation[i][j] = Integer.parseInt(input[j]);
      }
      if(nation[i][0] == K) {
        KIndex = i;
      }
    }

    for(int i = 0; i < N; i++) {
      if(KIndex == i) {
        continue;
      }
      for(int j = 1; j < 4; j++) {
        if(nation[i][j] > nation[KIndex][j]) {
          count++;
          break;
        } else if(nation[i][j] < nation[KIndex][j]) {
          break;
        }
      }
    }

    System.out.println(count + 1);

  }

}

 

 

GitHub 링크

https://github.com/MetroDefro/CodingTest_AutoSave/tree/main/%EB%B0%B1%EC%A4%80/Silver/8979.%E2%80%85%EC%98%AC%EB%A6%BC%ED%94%BD

 

CodingTest_AutoSave/백준/Silver/8979. 올림픽 at main · MetroDefro/CodingTest_AutoSave

모든 코딩 테스트 자동 저장. Contribute to MetroDefro/CodingTest_AutoSave development by creating an account on GitHub.

github.com

 

+ Recent posts