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

 

코딩테스트 연습 - 모의고사

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는

programmers.co.kr

 


풀이 과정

1, 2, 3번째 사람의 정답의 규칙성을 배열에 담았다. 1번은 5번째마다, 2번은 8번째마다, 3번은 10번째 마다 규칙이 반복되므로, 문제 번호를 5, 8, 10으로 나눠서 문제의 정답과 배열에 들어있는 수가 일치한지 확인해서 정답 여부를 판단하였다.

 

파이썬에서는 max(배열)

자바스크립트에서는 Math.max(...배열)을 통해서 배열에서의 최대값을 얻어낼 수 있고, 이를 통해 가장 높은 점수를 받은 사람을 판별하였다.


소스 코드

파이썬

def solution(answers):
    answer = []
    correct = [0, 0, 0]
    people1 = [1, 2, 3, 4, 5]
    people2 = [2, 1, 2, 3, 2, 4, 2, 5]
    people3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]

    for question in range(len(answers)):
        if answers[question] == people1[question % 5]:
            correct[0] += 1
        if answers[question] == people2[question % 8]:
            correct[1] += 1
        if answers[question] == people3[question % 10]:
            correct[2] += 1

    high_score = max(correct)

    for i in range(len(correct)):
        if correct[i] == high_score:
            answer.append(i+1)

    return answer

 

자바스크립트

function solution(answers) {
    var answer = [];
    correct = [0, 0, 0]
    people1 = [1, 2, 3, 4, 5]
    people2 = [2, 1, 2, 3, 2, 4, 2, 5]
    people3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]

    for (var question = 0; question < answers.length; question++) {
        if (answers[question] == people1[question % 5]) {
            correct[0] += 1
        }
        if (answers[question] == people2[question % 8]) {
            correct[1] += 1
        }
        if (answers[question] == people3[question % 10]) {
            correct[2] += 1
        }
    }

    high_score = Math.max(...correct)

    for (var i = 0; i < correct.length; i++) {
        if (correct[i] == high_score) {
            answer.push(i+1)
        }
    }

    return answer;
}

+ Recent posts