프로그래머스_C#/Level_1

[프로그래머스 C#] 모의고사

최애뎡 2021. 9. 7. 00:10
728x90
반응형

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

 

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

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

programmers.co.kr

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
    public int[] solution(int[] answers) {
        List <int> answer = new List<int>();
        
        int[] people = new int[3] { 0, 0, 0 };
        int[] person_1 = new int[] { 1, 2, 3, 4, 5 };
        int[] person_2 = new int[] { 2, 1, 2, 3, 2, 4, 2, 5 };
        int[] person_3 = new int[] { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
        
        for (int i = -1; ++i < answers.Length;)
        {            
            if (answers[i] == person_1[i % 5]) ++people[0];
            if (answers[i] == person_2[i % 8]) ++people[1];
            if (answers[i] == person_3[i % 10]) ++people[2];
        }
                
        for (int i = -1; ++i < people.Length;)
            if (people[i] == people.Max()) answer.Add(i + 1);

        return answer.ToArray();
    }
}

가장 높은 점수를 받은 사람이 여럿일 경우가 있기 때문에 그냥 answer을 List로 하고 정답 계산 후 정렬

반환은 다시 배열로

반응형