[프로그래머스] 모의고사 42840 (JAVA)

dia·2023년 11월 3일
0

풀이 방식

  1. 각 학생마다 배열을 만들어서 찍는 규칙을 넣어주기
  2. 정답 배열과 찍기 배열을 비교해서 각 학생 채점하기
  3. 가장 높은 점수 구하기
  4. 최고점을 맞은 학생들을 변수에 저장
  5. 변수를 자릿수로 분리해서 정답 배열 구하기

포인트

나머지 연산

시행 착오:
나머지 연산을 활용하는 것까진 알았는데 정확한 연산식은 헷갈림
알고보니 나머지 연산 말고는 필요가 없었음

해결 방안:
헷갈리면 하나씩 순서대로 적어보자
i값, 배열A의 인덱스 번호, 배열B의 인덱스 번호, 나머지 연산 값


구현

public class NUM42840 {
    public static void main(String[] args) {
        int[] answers = {5, 5, 5, 5, 5, 5, 5, 5};
        System.out.println(Arrays.toString(solution(answers)));
    }
    public static int[] solution(int[] answers) {
        int[] answer = {};
        int[] studentA = {1, 2, 3, 4, 5};
        int[] studentB = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] studentC = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int scoreA = 0, scoreB = 0, scoreC = 0;

        for(int i = 0; i < answers.length; i++) {
            if(studentA[i%5] == answers[i]) scoreA++;
            if(studentB[i%8] == answers[i]) scoreB++;
            if(studentC[i%10] == answers[i]) scoreC++;
        }

        int max = Math.max(scoreA, Math.max(scoreB, scoreC));
        int correct = 0; int count = 0;
        if(scoreA == max) { correct = 1; count++; }
        if(scoreB == max) { correct = correct * 10 + 2; count++; }
        if(scoreC == max) { correct = correct * 10 + 3; count++; }

        answer = new int[count];
        for(int i = 0; i < count; i++) {
            answer[count-i-1] = correct % 10;
            correct /= 10;
        }

        return answer;
    }
}

*다른 분들의 코드를 참고하여 작성했습니다

profile
자기 자신을 위한 CS 메모장 (그림은 지인분이 그려주신 것!)

0개의 댓글