개발 공부

(프로그래머스)(자바) 모의고사 본문

알고리즘

(프로그래머스)(자바) 모의고사

아이셩짱셩 2021. 7. 7. 16:22

코딩테스트 연습-완전탐색- 모의고사

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

import java.util.*;
import java.lang.*;

class Solution {
    public int[] solution(int[] answers) {      
        int[] answer = {};
        
        int[] scores = new int[3];
        int[] answer1 = {1,2,3,4,5};
        int[] answer2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] answer3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        scores[0] = 0;
        scores[1] = 0;
        scores[2] = 0;
        
        for(int i = 0 ; i < answers.length ; i++){
            if(answers[i] == answer1[i%answer1.length]){
                scores[0] += 1;        
            }
            if(answers[i] == answer2[i%answer2.length]){
                scores[1] += 1;        
            }
            if(answers[i] == answer3[i%answer3.length]){
                scores[2] += 1;        
            }
        }
        
        System.out.println(scores[0]+"/"+scores[1]+"/"+scores[2]);
        int max = Math.max(scores[0], Math.max(scores[1], scores[2]));
        
        
        List l = new ArrayList();
        for(int i = 0 ; i < 3 ; i++){
            if(scores[i] == max){
                l.add(i+1);
            }
        }
        answer = new int[l.size()];
        for(int i = 0 ; i < answer.length ; i++){
            answer[i] = (int)l.get(i);
        }
        
        return answer;
    }
}
Comments