본문 바로가기

CS/Algorithm

모의고사

반응형

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

 

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

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

programmers.co.kr

문제가 쉬운편이라 금방 풀었다~

효울성도 쏘쏘

package question;

import java.util.ArrayList;
import java.util.Collections;

public class 모의고사
{
    public static void main(String [] args)
    {
        int[] answers = {1, 3, 2, 4, 2};
        int [] result = solution(answers);
        System.out.println(result);
    }

    public static int[] solution(int[] answers)
    {
        int[] firstPattern = {1, 2, 3, 4, 5};
        int[] secondPattern = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] thirdPattern = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

        int firstCount = 0;
        int secondCount = 0;
        int thirdCount = 0;
        for(int i = 0; i < answers.length; i++)
        {
            if (firstPattern[i % firstPattern.length] == answers[i])
            {
                firstCount++;
            }
            if (secondPattern[i % secondPattern.length] == answers[i])
            {
                secondCount++;
            }
            if (thirdPattern[i % thirdPattern.length] == answers[i])
            {
                thirdCount++;
            }
        }

        int max = 0;
        if (firstCount >= secondCount && firstCount >= thirdCount)
        {
            max = firstCount;
        }
        if (secondCount >= firstCount && secondCount >= thirdCount)
        {
            max = secondCount;
        }
        if (thirdCount >= firstCount && thirdCount >= secondCount)
        {
            max = thirdCount;
        }

        ArrayList<Integer> answer = new ArrayList<Integer>();
        if (max == firstCount)
        {
            answer.add(1);
        }
        if (max == secondCount)
        {
            answer.add(2);
        }
        if (max == thirdCount)
        {
            answer.add(3);
        }

        Collections.sort(answer);
        int [] result = new int[answer.size()];
        for (int i = 0; i < answer.size(); i++)
        {
            result[i] = answer.get(i);
        }
        return result;
    }
}
반응형

'CS > Algorithm' 카테고리의 다른 글

카카오 문자열 압축 문제  (0) 2021.05.07
로또의 최고 순위와 최저 순위  (0) 2021.05.04
키패드 누르기  (0) 2021.05.04
신규 아이디 추천  (0) 2021.05.03
k 번째 수  (0) 2021.05.03