[알고리즘]/백준

자바 - 구현 - 백준 20113 긴급회의

broship 2021. 4. 22. 00:08

문제


 

 

 

 

 

 

문제해결


import java.util.Scanner;

public class B1_20113 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        //투표 받은 횟수 만들기
        int[] vote = new int[num];
        for (int i=0;i<num;i++){
            int tmp = sc.nextInt();
            if(tmp!=0)
                vote[tmp-1]++;
        }

        //최대값 구하기
        int max = 0;
        for (int i=0;i<num;i++){
            if(vote[i]>max)
                max = vote[i];
        }

        //최대값 중복 체크
        int cnt = 0;
        int idx = 0;
        for (int i=0;i<num;i++){
            if(vote[i]==max){
                cnt++;
                idx = i;
            }
        }
        //최대값이 하나라면 해당 사람 퇴출
        if(cnt==1)
            System.out.println(idx+1);
        else//최대값이 여럿일 경우 스킵
            System.out.println("skipped");
    }
}

1. int형 배열 vote를 만들어 각 사람마다 투표 받은 횟수를 기록한다

2. 그 중 가장 많이 투표 받은 횟수를 찾는다

3. 가장 많이 받은 횟수가 중복될 경우 스킵, 하나일 경우 해당 사람이 퇴출된다