Jam's story
[프로그래머스] 모의고사 본문
package soltest;
class Solution5 {
public int[] solution(int[] answers) {
int[] answer = {};
//반복되는 수로만 배열을 생성
int[] a= { 1, 2, 3, 4, 5};
int[] b= {2, 1, 2, 3, 2, 4, 2, 5};
int[] c= {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
//맞춘 갯수를 저장하는 배열
int[] score= {0,0,0};
//얼마나 맞췄는지
for (int i = 0; i < answers.length; i++) {
if(answers[i]==a[i%5]) score[0]++;
else if(answers[i]==b[i%8]) score[1]++;
else if(answers[i]==c[i%10]) score[2]++;
}
//누가 젤 많이 맞췄는지
int max=0;
for (int i = 0; i < score.length; i++) {
//max보다 더 큰 값이 생기면 max값이 그 값으로 바뀜
if(max<score[i]) max=score[i];
}
// 가장 높은 점수를 받은 사람 수 =max값에 해당하는사람
int maxCount = 0;
for(int i=0; i< score.length; i++){
if(score[i] == max){ maxCount++; }
}
//maxCount 수만큼 배열을 만들어주고
answer = new int[maxCount];
//값이 max인 것들을 반환할 answer 배열에 넣어주기
for(int i=0; i< score.length; i++){
//수포자 1,2,3의 그 숫자가 들어가니 i+1
if(score[i] == max) answer[i] = i+1;
}
return answer;
}
}
/*
* 1번: 1, 2, 3, 4, 5
*2번: 2, 1, 2, 3, 2, 4, 2, 5,
*3번: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 반복
*
* */
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 폰캣몬 (0) | 2022.06.24 |
---|---|
[프로그래머스 ] 체육복 (0) | 2022.06.23 |
[JAVA] - K번째수 (0) | 2022.06.21 |
[프로그래머스] 완주하지 못한 선수 (0) | 2022.06.20 |
[키패드 누르기] (0) | 2022.06.14 |
Comments