모의고사 문제
https://programmers.co.kr/learn/courses/30/lessons/42840
문제설명
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.
1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.
제한 조건
- 시험은 최대 10,000 문제로 구성되어있습니다.
- 문제의 정답은 1, 2, 3, 4, 5중 하나입니다.
- 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해주세요.
입출력 예
answer return [1, 2, 3, 4, 5] [1] [1, 3, 2, 4, 2] [1, 2, 3] 입출력 예 설명
- 입출력 예 #1
수포자 1은 모든 문제를 맞혔습니다.
수포자 2는 모든 문제를 틀렸습니다.
수포자 3은 모든 문제를 틀렸습니다.
따라서 가장 문제를 많이 맞힌 사람은 수포자 1입니다.- 입출력 예 #2
모든 사람이 2문제씩을 맞췄습니다.
import java.util.*;
class Solution {
public ArrayList solution(int[] answers) {
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};
// listA에 1번 수포자의 답안지를 answers 길이에 맞게 생성
ArrayList<Integer> listA = new ArrayList<>();
int indexA = 0;
int stdA = 0;
while(stdA < answers.length){
if(stdA % a.length == 0)
indexA = 0;
listA.add(a[indexA]);
indexA++;
stdA++;
}
// listB에 2번 수포자의 답안지를 answers 길이에 맞게 생성
ArrayList<Integer> listB = new ArrayList<>();
int indexB = 0;
int stdB = 0;
while(stdB < answers.length){
if(stdB % b.length == 0)
indexB = 0;
listB.add(b[indexB]);
indexB++;
stdB++;
}
// listC에 3번 수포자의 답안지를 answers 길이에 맞게 생성
ArrayList<Integer> listC = new ArrayList<>();
int indexC = 0;
int stdC = 0;
while(stdC < answers.length){
if(stdC % c.length == 0)
indexC = 0;
listC.add(c[indexC]);
indexC++;
stdC++;
}
// 1번, 2번, 3번 수포자들의 정답 개수 세기.
int cntA = 0;
int cntB = 0;
int cntC = 0;
for(int i = 0; i < answers.length; i++){
if(answers[i] == listA.get(i))
cntA++;
if(answers[i] == listB.get(i))
cntB++;
if(answers[i] == listC.get(i))
cntC++;
}
// 세 수포자에 최대 정답 개수 구하기.
int max = Math.max(Math.max(cntA, cntB), cntC);
// max와 개수가 똑같은 수포자 번호를 list에 넣기(같으면 오름차순)
ArrayList<Integer> list = new ArrayList<>();
if(max == cntA)
list.add(1);
if(max == cntB)
list.add(2);
if(max == cntC)
list.add(3);
return list;
}
}
import java.util.ArrayList;
class Solution {
public int[] solution(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 = new int[3];
for(int i=0; i<answer.length; i++) {
if(answer[i] == a[i%a.length]) {score[0]++;}
if(answer[i] == b[i%b.length]) {score[1]++;}
if(answer[i] == c[i%c.length]) {score[2]++;}
}
int maxScore = Math.max(score[0], Math.max(score[1], score[2]));
ArrayList<Integer> list = new ArrayList<>();
if(maxScore == score[0]) {list.add(1);}
if(maxScore == score[1]) {list.add(2);}
if(maxScore == score[2]) {list.add(3);}
return list.stream().mapToInt(i->i.intValue()).toArray();
}
}
if(answer[i] == a[i%a.length])를 통해 정답 개수를 세주었다.