반응형

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13zo1KAAACFAYh 

 

SW Expert Academy

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

swexpertacademy.com

 

풀이방법

1. 0~100점까지를 0~100인덱스의 배열로 두어 해당 점수가 입력값으로 올 때 해당 배열의 인덱스 == 점수라고 생각하고 누적하여 +1을 계속 더해준다.

2. 배열의 0~100인덱스를 탐색하며 가장 높은 값을 찾아 해당 인덱스(점수) 출력.

느낀점

입력값을 받을 때 BufferedReader 모듈을 사용했는데, 또 다른 풀이를 보면 Scanner를 사용하는 경우도 있다. 또한, 입력값이 공백으로 나뉜 경우에는 split()을 사용하지 않고, ~~Token(?) 모듈을 사용하는 경우도 있었다.

만약에 python이었을 경우 그냥 int(input() 혹은 map(int,input().split())으로 두 가지의 경우를 간단하게 나타낼 수 있는데, JAVA는 입력값을 여러가지의 수로 나타낼 수 있는 것 같았다.

이러한 이유에 대해 따로 구글링을 하여 찾아보고, 더 궁금한 부분은 전임교수님께 질문드릴 예정.

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
 
public class Solution{
     
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
         
        //결과를 한 번에 출력하기 위함
        StringBuilder sb = new StringBuilder();
         
        int T;
        T = Integer.parseInt(in.readLine());
        for(int test_case = 1; test_case <= T; test_case++) {
            sb.append("#" + test_case + " ");
             
            int caseNum = Integer.parseInt(in.readLine());
             
            int N = 1000;
            int[] scores = new int[N];
             
            String readLine = in.readLine();
            String[] split = readLine.split(" ");
            for (int i = 0; i < N; i++) {
                scores[i] = Integer.parseInt(split[i]);
            }
             
            int result = 0;
             
            int[] arr = new int[101];
            for(int tmp : scores) {
                arr[tmp]++;
            }
            int mx = 0;
            for(int j=0;j<101;j++) {
                 
                if (mx <= arr[j]) {
                    mx = arr[j];
                    result = j;
                }
            }
             
            sb.append(result).append("\n");
        }
         
        System.out.println(sb);
    }
}

+ Recent posts