카테고리 없음
모의고사
byeol2ing
2021. 9. 17. 20:45
반응형
이문제는 나머지를 구하여 해당 번호의 답을 찾아내는것이 관건이었던 것 같다.
import java.util.*; class Solution { public int[] solution(int[] answers) { int[] one = {1,2,3,4,5}; int[] two = {2,1,2,3,2,4,2,5}; int[] three = {3,3,1,1,2,2,4,4,5,5}; int[] score = {0,0,0}; for(int i=0; i<answers.length; i++){ if(one[i%one.length] == answers[i]){ score[0] ++; } if(two[i%two.length] == answers[i]){ score[1] ++; } if(three[i%three.length] == answers[i]){ score[2] ++; } } ArrayList<Integer> list = new ArrayList(); int max = Math.max(score[2],Math.max(score[0],score[1])); for(int i=0; i<score.length; i++){ if(max == score[i]){ list.add(i+1); } } int[] answer = new int[list.size()]; for(int i=0; i<list.size(); i++){ answer[i] = list.get(i) ; } return answer; } } |
반응형