프로그래머스 코딩테스트/JAVA

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

Coding-Su 2024. 8. 8. 14:42
728x90

문제

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

정답

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        int[] answer;
        int[] person1 = {1, 2, 3, 4, 5};
        int[] person2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] person3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        List<Integer> score = new ArrayList<>();
        List<Integer> ans = new ArrayList<>();
        score.add(0);
        score.add(0);
        score.add(0);
        
        for(int i = 0; i < answers.length; i++) {
            if(answers[i] == person1[i%5]) 
                score.set(0, score.get(0)+1);
            if(answers[i] == person2[i%8])
                score.set(1, score.get(1)+1);
            if(answers[i] == person3[i%10])
                score.set(2, score.get(2)+1);
        }
        int max = Collections.max(score);
        for(int i = 0; i < 3; i++) {
            if(max == score.get(i)) {
                ans.add(i+1);
            }
        }
        
        answer = new int[ans.size()];
        
        for(int i = 0; i < ans.size(); i++) {
            answer[i] = ans.get(i);
        }
        
        return answer;
    }
}

 

처음에 1번 수포자, 2번 수포자, 3번 수포자가 입력하는 값을 미리 배열에 저장해두었습니다. 

 

다음으로 score를 저장할 list를 만들고 0으로 초기화를 했습니다.

 

for문을 돌면서 나머지 연산자를 통해 정답이 맞다면 score 값이 증가하도록 작성했습니다.

 

가장 큰 값을 구하고 만약 큰 값고 같은 값이 있으면 인덱스 번호를 저장하여 문제를 풀었습니다.

728x90