모의고사

모의고사
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// https://programmers.co.kr/learn/courses/30/lessons/42840
function solution(answers) {
const a = [1, 2, 3, 4, 5];
const b = [2, 1, 2, 3, 2, 4, 2, 5];
const c = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

let count = [0, 0, 0];
answers.forEach((element, index) => {
a[index % a.length] == element ? count[0]++ : null;
b[index % b.length] == element ? count[1]++ : null;
c[index % c.length] == element ? count[2]++ : null;
});
let result = [];
let max = Math.max.apply(null, count);
count.forEach((item, idx) => {
if (max == item) {
result.push(idx + 1);
}
});
return result;
}
solution([1, 2, 3, 4, 5]);

해설

  • 입력으로 정답 배열이 들어온다
  • a,b,c 세 사람이 있다
  • 사람마다 찍기 타입이 있다
  • 각자 자신만의 패턴으로 문제를 찍었을때 가장 문제를 많이 맞힌 사람을 반환한다
  • 일단 찍기타입을 정의하고,
  • 각 사람마다 정답이면 카운트를 늘린다!
  • 나머지 연산으로 패턴을 지속한다. c[index % c.length]
  • 그 중에서 가장 많이 맞춘 사람 배열을 반환한다. (1~다수)
Author

chinsung

Posted on

2020-09-18

Updated on

2021-08-04

Licensed under

댓글