슈콩

[프로그래머스] Lv.1 모의 고사 본문

Algorithms/Programmers

[프로그래머스] Lv.1 모의 고사

shukong 2025. 10. 9. 21:50

 

 

[문제]

https://school.programmers.co.kr/learn/courses/30/lessons/42840

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

[소스 코드]

import java.util.*;
class Solution {
    static class Exam{
        int num,score;
        Exam(int num,int score){
            this.num = num;
            this.score = score;
        }
    }
    public int[] solution(int[] answers) {
        int n = answers.length;
        Exam[] exams = new Exam[3];
        for(int i=0;i<3;i++){
            exams[i] = new Exam(i+1,0);
        }
        int[] math1 = {1,2,3,4,5};
        int[] math2 = {2,1,2,3,2,4,2,5};
        int[] math3 = {3,3,1,1,2,2,4,4,5,5};
        for(int i=0;i<n;i++){
            if(math1[i%5]==answers[i]) exams[0].score++;
            if(math2[i%8]==answers[i]) exams[1].score++;
            if(math3[i%10]==answers[i]) exams[2].score++;
        }
        
        List<Integer> list = new ArrayList<>();
        int maxScore = Math.max(Math.max(exams[0].score,exams[1].score),exams[2].score);
        for(int i=0;i<3;i++){
            if(maxScore==exams[i].score) list.add(exams[i].num);
        }
        Collections.sort(list);
         int[] answer = new int[list.size()];
        for(int i=0;i<list.size();i++){
            answer[i] = list.get(i);
        }
        return answer;
    }
}