티스토리 뷰

문제 링크

모의고사

문제 조건

  • 시험은 최대 10,000 문제로 구성되어 있습니다.
  • 문제의 정답은 1, 2, 3, 4, 5 중 하나입니다.
  • 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해야 합니다.

변수 설명

  • answers : 1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 (매개변수)
  • one : 1번 수포자가 찍는 방식
  • oneScore : 1번 수포자의 점수
  • two : 2번 수포자가 찍는 방식
  • twoScore : 2번 수포자의 점수
  • three : 3번 수포자가 찍는 방식
  • threeScore : 3번 수포자의 점수
  • len : answer의 길이

코드 설명

        int[] one = {1, 2, 3, 4, 5};
        int[] two = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] three = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
  • 수포자들의 찍는 방식을 구하여 배열에 넣어줍니다.
        for(int i=0;i<len;i++){
            if(answers[i] == one[i%5]) oneScore++;
            if(answers[i] == two[i%8]) twoScore++;
            if(answers[i] == three[i%10]) threeScore++;
        }
  • [1]에서 구한 배열과 answer을 비교하여 수포자들의 점수를 구합니다.
       int maxScore = Math.max(oneScore, Math.max(twoScore, threeScore));
        ArrayList<Integer> list = new ArrayList<>();
        if(maxScore == oneScore)list.add(1);
        if(maxScore == twoScore)list.add(2);
        if(maxScore == threeScore)list.add(3);
        int lLen = list.size();
        answer = new int[lLen];
        for(int i=0;i< lLen;i++){
            answer[i] = list.get(i);
        }
        }
  • maxScore를 구한 뒤, list에 넣어준 뒤 answer에 맞는 타입으로 변환하여 넣어줍니다.

문제 해답

import java.util.ArrayList;

class Solution {
    public int[] solution(int[] answers) {
        int[] answer;
        int[] one = {1, 2, 3, 4, 5};
        int[] two = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] three = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int oneScore = 0, twoScore = 0, threeScore = 0;

        int len = answers.length;
        for(int i=0;i<len;i++){
            if(answers[i] == one[i%5]) oneScore++;
            if(answers[i] == two[i%8]) twoScore++;
            if(answers[i] == three[i%10]) threeScore++;
        }
        int maxScore = Math.max(oneScore, Math.max(twoScore, threeScore));
        ArrayList<Integer> list = new ArrayList<>();
        if(maxScore == oneScore)list.add(1);
        if(maxScore == twoScore)list.add(2);
        if(maxScore == threeScore)list.add(3);
        int lLen = list.size();
        answer = new int[lLen];
        for(int i=0;i< lLen;i++){
            answer[i] = list.get(i);
        }
        return answer;
    }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/01   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함