본문 바로가기
ALGORITHM/Sorting

[JAVA] 백준 8979번

by sjs_2215 2019. 6. 2.

https://www.acmicpc.net/problem/8979 올림픽 -정렬

[백준 8979번] 올림픽

문제 이해:

올림픽 메달 개수로 순위 매기기

답 봄 . 2차원 배열로 한 사람

https://yeolco.tistory.com/8


내가 하려고 한 것:

2차원 배열에 메달 정보 저장.

정렬 클래스 따로 작성.

메인에서 금메달을 내림차순 정렬&은메달 내림차순 정렬&동메달 내림차순 정렬을 함.

금메달 내림차순 정렬한 것대로 순위 매김.

but 금메달 동점 국가가 있다면 은메달 내림차순 정렬된 것으로 순위 매김

but 은메달 동점 국가가 있다면 동메달 내림차순 정렬된 것으로 순위 매김 해서 답을 내려고 했음.

근데, 'but' 부분이 생각대로 안되서 포기하고 답을 봄

안 된 부분 ex) 금메달 따로 정렬. 은메달 따로 정렬 동메달 따로 정렬됨.

금메달 개수 똑같아서 은메달 개수로만 비교하는 게 안됨.


내가 생각 못한 것:

  • 메달 개수가 같을 때의 동점 국가 처리에 대해 많은 고민을 함 -> 이 고민을 아예 안 해도 되는 방법이 있었음 -> 금/은/동에 각각 점수를 부여하면 복잡한 동점자 처리 안해도 됨. 단순히 점수 비교만 하면 됨.
  • 순위 알고 싶은 나라를 먼저 저장하고, 그 나라에 대해 순위 계산을 하는 것이 훨씬 빠르다는 것을 생각 못함. 그래서 코드가 길어짐
  • 아이디어만 생각하고 그 아이디어를 어떻게 '실현시킬지'에 대해 깊이 생각하지 않고 또 무작정 코딩함. 그래서 디버깅 시간만 낭비함.
  • 문제를 보고 아 이 문제는 정렬 문제다 라고 생각했는데, 정렬로 푸는 방법도 알고 싶음.

점수를 7점-5점-1점으로 줌-> 틀림

틀린 코드

package till;
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n=scan.nextInt(); //나라 수
        int k=scan.nextInt(); //알고싶은 나라 번호
        int result=1;
        int[][] medals = new int[n][4];
        int[][] score_medals = new int[n][2];//가중치 점수 부여된 배열

        for(int i=0;i<n;i++) {
            for(int j=0;j<4;j++){
            medals[i][j]=scan.nextInt(); //국가들의 메달 정보를 저장
            }
        }

        for(int i=0;i<n;i++) {
            score_medals[i][0]=medals[i][0]; //국가 번호는 그냥 복붙
            score_medals[i][1]=(medals[i][1]*7)+(medals[i][2]*5)+(medals[i][3]*1); //score_medals[국가번호][1]위치에 각 국가의 점수를 저장
        }

        /*
        //출력 잘되나 확인
        for(int i=0;i<n;i++) {
            for(int j=0;j<2;j++){
            System.out.print(score_medals[i][j]+", ");
            }
            System.out.println("");
        }

        */    

        for(int i=0;i<n;i++) {
            if(score_medals[i][0]==k){
                for(int j=0;j<n;j++){
                    if(score_medals[i][1]<score_medals[j][1])
                        result++;
                }
            }
        }

        //k나라 순위 알려주기
            System.out.println(result); 

        }
    }

틀린 이유:

  • 점수를 저렇게 조금씩 하면 안 됨
  • 점수 크게 주면 에러 남 (자료형 out of range 에러)

이 에러는 해결함. 아래 사진 참고 (출처: 생활코딩)


파이널 코드! 해결!

package till;
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n=scan.nextInt(); //나라 수
        int k=scan.nextInt(); //알고싶은 나라 번호
        int result=1;
        int[][] medals = new int[n][4];
        long[][] score_medals = new long[n][2];//가중치 점수 부여된 배열

        for(int i=0;i<n;i++) {
            for(int j=0;j<4;j++){
            medals[i][j]=scan.nextInt(); //국가들의 메달 정보를 저장
            }
        }

        for(int i=0;i<n;i++) {
            score_medals[i][0]=medals[i][0]; //국가 번호는 그냥 복붙
            score_medals[i][1]=(medals[i][1]*1000000000000L)+(medals[i][2]*1000000)+(medals[i][3]*1); //score_medals[국가번호][1]위치에 각 국가의 점수를 저장
        }

        /*
        //출력 잘되나 확인
        for(int i=0;i<n;i++) {
            for(int j=0;j<2;j++){
            System.out.print(score_medals[i][j]+", ");
            }
            System.out.println("");
        }

        */    

        for(int i=0;i<n;i++) {
            if(score_medals[i][0]==k){
                for(int j=0;j<n;j++){
                    if(score_medals[i][1]<score_medals[j][1])
                        result++;
                }
                break;
            }
        }

        //k나라 순위 알려주기
            System.out.println(result); 
        }
    }

팀원 풀이 :

import java.util.Scanner;


public class problem8979 {

   static int n;
   static int k;



   public static void main (String[] args) {
      Scanner sc = new Scanner(System.in);
      n= sc.nextInt();
      k= sc.nextInt();
      Medal[] country = new Medal[n];

      for(int i=0;i<n;i++) {
         country[i] = new Medal();
         country[i].name= sc.nextInt();
         country[i].gold = sc.nextInt();
         country[i].silver = sc.nextInt();
         country[i].bronze = sc.nextInt();
      }
      sc.close();

      int result = CountryRank(country);
      System.out.println(result);
   }

   public static int CountryRank(Medal[] country) {
      int rank = 1;
      int answer = 0;
      for(int j=0;j<n;j++) {
         if(country[j].name == k)
            answer = j;
      }

      for(int i=0;i<n;i++) {
         if(country[i].gold>country[answer].gold)
            rank++;
         else if((country[i].gold==country[answer].gold)&&(country[i].silver>country[answer].silver))
            rank++;
         else if((country[i].gold==country[answer].gold)&&(country[i].silver==country[answer].silver)&&(country[i].bronze>country[answer].bronze))
            rank++;

      }
      return rank;

   }

   private static class Medal{
      int name;
      int gold;
      int silver;
      int bronze;


   }
}

=> '자바'를 활용하려 노력하자

'ALGORITHM > Sorting' 카테고리의 다른 글

[JAVA] 프로그래머스 H-Index  (0) 2019.06.24
[JAVA] 프로그래머스 K번째수  (0) 2019.06.24

Comments