https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13zo1KAAACFAYh&categoryId=AV13zo1KAAACFAYh&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

이 문제는 학생수 1000명, 점수는 0점에서 100점으로 고정되어 있다.

주의할 점은 최빈수가 여러 개 일 때에는 가장 큰 점수를 출력해야 한다.

 

풀이

  1. 점수를 세기 위한 count배열을 만들어 놓는다. 0점부터 100점이므로 크기는 101로 해야 한다.
  2. 각 학생의 점수를 입력받아서 해당 점수를 index 값으로 count배열의 값을 1씩 증가시킨다.
  3. 학생의 점수를 모두 입력받았으면 count배열에 최댓값과 그때의 index(점수)를 구해준다.
  4. 이때 max값을 구하는 부분에서 <가 아닌 <= 를 사용해서 최빈수 중 가장 큰 점수가 저장되게 한다. (max 값이 같은 경우에 더 뒤의 값(더 큰 점수)으로 바뀌도록 해준다.)
import java.util.StringTokenizer;
import java.io.*;

public class Solution {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		for(int tc=1; tc<=T; tc++) {
			int num = Integer.parseInt(br.readLine());
			int[] count = new int[101];
			
			StringTokenizer st = new StringTokenizer(br.readLine());
			int score = 0;
			for(int i=0; i<1000; i++) {
				score = Integer.parseInt(st.nextToken());
				count[score] += 1;
			}
			
			int max = 0; //최댓값을 저장
			int ans = 0; //최댓값을 가지고 있는 index(점수)
			for(int i=0; i<101; i++) {
				if(max <= count[i]) {
					max = count[i];
					ans = i;
				}
			}
			
			System.out.println("#"+tc+" "+ans);
		}
	}

}

'SWEA > D2' 카테고리의 다른 글

[SWEA] 1284. 수도 요금 경쟁  (0) 2019.05.19
[SWEA] 1288. 새로운 불면증 치료법  (0) 2019.05.16
[SWEA] 1928. Base64 Decoder  (0) 2019.05.16
[SWEA] 1859. 백만 장자 프로젝트  (1) 2019.05.16
[SWEA] 1940. 가랏! RC카!  (0) 2019.05.14

+ Recent posts