본문으로 바로가기

✍️ 모의고사

📘 문제 설명

수학을 포기한 세 명의 학생이 각각 자신만의 일정한 규칙으로 문제를 찍습니다. 시험의 정답이 담긴 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 반환하는 문제입니다. 만약 가장 높은 점수를 받은 사람이 여러 명이라면 오름차순으로 정렬하여 반환해야 합니다.

📌 제한조건

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

💡 개념 설명

  • 알고리즘 핵심: 각 수포자의 찍기 패턴을 배열로 정의하고, 나머지 연산(%)을 사용하여 문제 번호에 맞는 정답을 순환적으로 확인하는 것이 핵심입니다. * 로직 단계:
    1. 패턴 정의: 1번(1,2,3,4,5), 2번(2,1,2,3,2,4,2,5), 3번(3,3,1,1,2,2,4,4,5,5)의 반복 패턴을 배열에 담습니다.
    2. 점수 계산: answers 배열을 순회하며 i % 패턴길이 인덱스를 사용해 수포자의 답과 실제 정답을 비교하여 각자의 점수를 카운트합니다.
    3. 최대 점수 찾기: 세 명의 점수 중 가장 높은 점수(maxScore)를 구합니다.
    4. 결과 생성: 최대 점수와 일치하는 점수를 가진 학생의 번호를 리스트에 추가한 뒤, 배열로 변환하여 반환합니다.

📎 입출력 예시

💻 프로그래머스 제출용 코드

import java.util.*;

class Solution {
    public int[] solution(int[] answers) {
        // 1. 수포자들의 찍기 패턴 정의
        int[] p1 = {1, 2, 3, 4, 5};
        int[] p2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] p3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

        int[] score = new int[3]; // 점수 저장용 배열

        // 2. 정답 확인 및 점수 누적
        for (int i = 0; i < answers.length; i++) {
            if (answers[i] == p1[i % p1.length]) score[0]++;
            if (answers[i] == p2[i % p2.length]) score[1]++;
            if (answers[i] == p3[i % p3.length]) score[2]++;
        }

        // 3. 가장 높은 점수 찾기
        int maxScore = Math.max(score[0], Math.max(score[1], score[2]));

        // 4. 최대 점수와 같은 점수를 받은 학생들 리스트에 추가
        List<Integer> list = new ArrayList<>();
        if (maxScore == score[0]) list.add(1);
        if (maxScore == score[1]) list.add(2);
        if (maxScore == score[2]) list.add(3);

        // 5. 리스트를 배열로 변환 (오름차순은 추가 순서대로 보장됨)
        int[] answer = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            answer[i] = list.get(i);
        }

        return answer;
    }
}

🖥️ IntelliJ 실행용 코드

import java.util.*;

public class Solution {
    public int[] solution(int[] answers) {
        int[] p1 = {1, 2, 3, 4, 5};
        int[] p2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] p3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

        int[] score = new int[3];

        for (int i = 0; i < answers.length; i++) {
            if (answers[i] == p1[i % p1.length]) score[0]++;
            if (answers[i] == p2[i % p2.length]) score[1]++;
            if (answers[i] == p3[i % p3.length]) score[2]++;
        }

        int maxScore = Math.max(score[0], Math.max(score[1], score[2]));

        List<Integer> list = new ArrayList<>();
        if (maxScore == score[0]) list.add(1);
        if (maxScore == score[1]) list.add(2);
        if (maxScore == score[2]) list.add(3);

        int[] answer = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            answer[i] = list.get(i);
        }

        return answer;
    }

    public static void main(String[] args) {
        Solution sol = new Solution();

        // 테스트 케이스 1
        int[] answers1 = {1, 2, 3, 4, 5};
        System.out.println("결과 1: " + Arrays.toString(sol.solution(answers1))); // [1]

        // 테스트 케이스 2
        int[] answers2 = {1, 3, 2, 4, 2};
        System.out.println("결과 2: " + Arrays.toString(sol.solution(answers2))); // [1, 2, 3]
    }
}

📎 결과