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

당당·2023년 5월 15일
0

프로그래머스

목록 보기
101/245

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

📔문제

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

1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.


🚫제한사항

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


📝입출력 예

answersreturn
[1,2,3,4,5][1]
[1,3,2,4,2][1,2,3]

입출력 예 #1

수포자 1은 모든 문제를 맞혔습니다. 수포자 2는 모든 문제를 틀렸습니다. 수포자 3은 모든 문제를 틀렸습니다. 따라서 가장 문제를 많이 맞힌 사람은 수포자 1입니다.

입출력 예 #2

모든 사람이 2문제씩을 맞췄습니다.


🧮알고리즘 분류

  • 브루트포스 알고리즘

📃소스 코드

class Solution {
    public int[] solution(int[] answers) {
        int[] answer = {};
        
        int[] first=new int[answers.length];
        int start=1;
        for(int i=0;i<answers.length;i++){
            first[i]=start;
            start++;
            if(start>5){
                start=1;
            }
        }
        
        int[] second=new int[answers.length];
        int se=1;
        for(int i=0;i<answers.length;i++){
            if(i%2==0){
                second[i]=2;
            }
            else{
                second[i]=se;
                se++;
                if(se>5){
                    se=1;
                }
                else if(se==2){
                    se=3;
                }
            }
        }
        
        int[] third=new int[answers.length];
        int thi=3;
        int count=0;
        for(int i=0;i<answers.length;i++){
            third[i]=thi;
            count++;
            
            if(count==2){
                if(thi==3){
                    thi=1;
                }
                else{
                    thi++;
                    if(thi==3){
                        thi=4;
                    }
                    else if(thi>5){
                        thi=3;
                    }
                }
                
                count=0;
            }
        }
        
        int one=0,two=0,three=0;
        for(int i=0;i<answers.length;i++){
            if(answers[i]==first[i]){
                one++;
            }
            if(answers[i]==second[i]){
                two++;
            }
            if(answers[i]==third[i]){
                three++;
            }
        }
        
        if(one>two){
            if(one>three){
                answer=new int[1];
                answer[0]=1;
            }
            else if(one<three){
                answer=new int[1];
                answer[0]=3;
            }
            else{
                answer=new int[2];
                answer[0]=1;
                answer[1]=3;
            }
        }
        else if(one<two){
            if(two>three){
                answer=new int[1];
                answer[0]=2;
            }
            else if(two<three){
                answer=new int[1];
                answer[0]=3;
            }
            else{
                answer=new int[2];
                answer[0]=2;
                answer[1]=3;
            }
        }
        else{
            if(two>three){
                answer=new int[2];
                answer[1]=2;
                answer[0]=1;
            }
            else if(two<three){
                answer=new int[1];
                answer[0]=3;
            }
            else{
                answer=new int[3];
                answer[0]=1;
                answer[1]=2;
                answer[2]=3;
            }
        }
        return answer;
    }
}

📰출력 결과


📂고찰

1번 수포자는 1~5까지 반복,
2번 수포자는 21232425가 반복,
3번 수포자는 3311224455가 반복된다.

1번 수포자는 1부터 계속 반복해주고 5를 넘으면 1로 돌려주면 된다.
2번 수포자는 i가 짝수일 때는 2를, 그렇지 않으면 1,3,4,5를 반복하면 되는데 만약 2가 되면 바로 3으로 넘겨주면 되고, 5를 넘으면 1로 돌려준다.
3번 수포자는 3번부터 시작해 count를 하고 만약 count가 2가 되면 다음 수로 넘어가는데 만약 3이면 1로 넘어간다. 그다음 1, 2를 실행한 후 2에서 3으로 넘어가려고 할 때 만약 3이면 바로 4로 넘어가면 된다.

profile
MySQL DBA 신입 지원

0개의 댓글