반응형

문제는 다음 사이트에서 풀었습니다.

programmers.co.kr/learn/courses/30/lessons/42840?language=java

 

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

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

programmers.co.kr

풀이 및 코드

import java.util.*;

class Solution {
  public int[] solution(int[] answers) {

        int[] result;

        //각자의 찍기 방식
        int[] supo1 = {1, 2, 3, 4, 5};
        int[] supo2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] supo3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

        //매번 length 를 계산하지 않기 위해 변수 사용
        int answerCnt = answers.length;

        // 각 수포자의 점수를 담을 배열
        int[] score = new int[3];

        // 답지를 돌며 비교해보자
        for (int i = 0; i < answerCnt; ++i) {
            //1번 수포자
            if (answers[i] == supo1[i % 5])
                ++score[0];

            //2번 수포자
            if (answers[i] == supo2[i % 8])
                ++score[1];

            //3번 수포자
            if (answers[i] == supo3[i % 10])
                ++score[2];
        }

        List<Integer> list = new ArrayList<>();

        //1번이 1등
        if(score[0] >= score[1] && score[0] >= score[2]){
            list.add(1);
            if(score[0] == score[1]){
                list.add(2);
            }
            if(score[0] == score[2]){
                list.add(3);
            }
        }
        //2번이 1등
        else if(score[1] > score[0] && score[1] >= score[2]){
            list.add(2);
            if(score[1] == score[2]){
                list.add(3);
            }
        }
        //3번이 1등
        else if(score[2] > score[0] && score[2] > score[1]){
            list.add(3);
        }

        result = new int[list.size()];

        for(int i = 0; i < list.size(); i++){
            result[i] = list.get(i);
        }

        return result;
    }
}

실행결과

실행결과

 

반응형

'개발 > 알고리즘' 카테고리의 다른 글

[USACO] Swapity Swap JAVA  (0) 2021.02.23
[USACO] Triangles JAVA  (0) 2021.02.22
[프로그래머스] 완주하기 못한 선수 JAVA  (0) 2021.02.20
[USACO] Daisy Chains (C언어)  (0) 2021.02.17
[USACO] Do You Know Your ABCs? (C언어)  (0) 2021.02.17
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기