[プログラマー/Javascript]シミュレーション試験(完全ナビゲーション)


プログラマコードテストはLvを完全に探索する.模擬試験
Javascript
質問する
수포자는 수학을 포기한 사람의 준말입니다. 
수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 
수포자는 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 함수를 작성해주세요.
条件
1. 시험은 최대 10,000 문제
2. 문제의 정답은 1, 2, 3, 4, 5중 하나
3. 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬
に答える
function solution(answers) {
  let answer = [];
  let leg = answers.length; // 정답배열의 길이
  let one = [], // 1번 수포자의 답배열
  	two = [], // 2번 수포자의 답배열
  	three = []; // 3번 수포자의 답배열
   
  // 각 수포자의 답배열에 정답배열길이만큼의 답을 각자의 규칙에 맞춰 넣기
  for (let i = 1; i <= leg; i++) {
  	one.push(i % 5 === 0 ? 5 : i % 5);
  	let a = i / 2 - 1;
  	two.push(i % 2 !== 0 ? 2 : a % 4 === 0 ? 1 : a % 4 === 1 ? 3 : a % 4 === 2 ? 4 : 5);
  	let b = Math.ceil(i / 2) - 1;
  	three.push(b % 5 === 0 ? 3 : b % 5 === 1 ? 1 : b % 5 === 2 ? 2 : b % 5 === 3 ? 4 : 5);
  }
  
  // 각자의 답배열 중 맞는답만 골라서 추출
  one = one.filter((n, index) => n === answers[index]);
  two = two.filter((n, index) => n === answers[index]);
  three = three.filter((n, index) => n === answers[index]);

  // 각자의 정답갯수를 비교하여 answer에 push
  if (one.length > two.length) {
    if (one.length > three.length) {
    	answer.push(1);
    } else if (one.length === three.length) {
    	answer.push(1);
    	answer.push(3);
    } else {
    	answer.push(3);
    }
  } else if (one.length === two.length) {
    if (one.length > three.length) {
      answer.push(1);
      answer.push(2);
    } else if (one.length === three.length) {
      answer.push(1);
      answer.push(2);
      answer.push(3);
    } else {
    	answer.push(3);
    }
  } else {
    if (two.length > three.length) {
    	answer.push(2);
    } else if (two.length === three.length) {
      answer.push(2);
      answer.push(3);
    } else {
    	answer.push(3);
    }
  }
  return answer;
}
別の解釈
function solution(answers) {
    var answer = [];
    
    // 각 수포자의 규칙에 맞는 답안배열 작성
    var a1 = [1, 2, 3, 4, 5];
    var a2 = [2, 1, 2, 3, 2, 4, 2, 5]
    var a3 = [ 3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

    // 정답배열에서 각 수포자의 배열 중 맞은 답만 추출해서 정답갯수로 반환
    var a1c = answers.filter((a,i)=> a === a1[i%a1.length]).length;
    var a2c = answers.filter((a,i)=> a === a2[i%a2.length]).length;
    var a3c = answers.filter((a,i)=> a === a3[i%a3.length]).length;
    
    // 각자의 정답갯수 중 가장 큰 숫자(3개중 가장 많이 맞은 답갯수) 추출
    var max = Math.max(a1c,a2c,a3c);

    // 각자의 답안갯수가 가장 많이 맞은 답갯수와 같을 때 answer에 push
    if (a1c === max) {answer.push(1)};
    if (a2c === max) {answer.push(2)};
    if (a3c === max) {answer.push(3)};

    return answer;
}
New idea

  • Math.max();
    3名の正解個数を比較した部分では、max()を使って簡単に個数守備交ができます!

  • 3人じゃなくて100人で1000万人以上だったら…?
    現在の問題は3人1組のルールがあるが,無数の人のルールから適切な答えを抽出する必要がある場合,上記のコードは解決できない.
    次に、入力したルールに基づいて正しい答えの数を抽出するアルゴリズムを作成します.
  • 2021.04.27