[アルゴリズム]模擬試験


問題の説明


数学は数学を放棄する人の略語である.「囚人3人組」は模擬試験で数学の問題を全部撮りたいと思っている.最初の問題から最後の問題まで、執胞子は以下の通りである.
1番捕手の撮り方:1,2,3,4,5,1,2,3,4,5...
2番捕手の撮り方:2、1、2、3、2、4、2、5、2、2、3、2、4、2、5...
3番捕手の撮り方:3,3,1,1,2,2,4,5,5,3,3,1,2,2,4,5,5...
最初の問題から最後の問題までの正解が順番に並んでいる場合は、最も多くの質問に答えた人が誰なのか、答えを並べて返すように解答関数を書いてください.

せいげんじょうけん

  • 試験には最大10000問が含まれています.
  • 題の正解は1,2,3,4,5の1つです.
  • 点数が一番高い人が何人かいる場合は、返される値を昇順に並べてください.
  • アルゴリズム#アルゴリズム#


    まず、
  • 試験箱(小胞子1〜3)をリストした.
  • の答えにtotal(正解の質問数)を加えます.
  • 回答(解答)の並び順で、正解の質問数を知るために回ります.
  • の高得点を把握するために、総点を基準に並べ替えます.
  • reduceを使用して、スコアが同じ場合はソート順に結果に入れます.
  • に何も含まれていない場合、高得点者は1名、すなわちaccの最初の因子を返します.
  • インプリメンテーション

    function solution(answers) {
        var answer = [
           { id : 1,
              total : 0 },
            { id : 2,
              total : 0 },
            { id : 3,
              total : 0 }
        ];
    
        let test1 = [1,2,3,4,5];
        let test2 = [2, 1, 2, 3, 2, 4, 2, 5];
        let test3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
    
        for(let i = 0; i < answers.length; i++) {
          // % 를 이용한 이유는 '패턴 배열의 길이'만큼 다시 돌아야하기 때문에 이용한다.
            if (answers[i] === test1[i % 5]) answer[0].total++;
            if (answers[i] === test2[i % 8]) answer[1].total++;
            if (answers[i] === test3[i % 10]) answer[2].total++;
        }
    
        answer.sort((a,b) => b.total-a.total);
    
        let result = [];
    
        answer.reduce(function(acc, current) {
           if (acc.total === current.total) {
              result.push(current.id);
         }
        return acc;
        }, answer[0]);
    
        return result;
    }
    参照後のリプレイ
            var answer = [];
        // 수포자가 찍는 패턴을 모은 배열을 정의하고
        let test1 = [1,2,3,4,5];
        let test2 = [2, 1, 2, 3, 2, 4, 2, 5];
        let test3 = [ 3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
        // 점수를 담을 배열을 정의한다.
        let total = [0, 0, 0]
        // answers의 길이만큼 돌면서, 각각의 맞춘 문제 수를 카운팅한다.
        for(let i=0; i < answers.length; i++){
          if (answers[i] === test1[i % 5]) total[0]++;
          if (answers[i] === test2[i % 8]) total[1]++;
          if (answers[i] === test3[i % 10]) total[2]++;  
        }
    
        // 고득점자를 가려내야한다.
        let maxTotal = Math.max(...total);
        // 가장 높은 점수를 받은 사람이 여럿일 경우, 순차적으로 넣어준다.
    
    
        for(let a=0; a < 3; a++){
            if(maxTotal === total[a]) answer.push(a+1);
        }
    
        return answer;

    他人の解答


    filterとMath.maxを使う方法があるなんて...呜呜...
    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;
        var max = Math.max(a1c,a2c,a3c);
    
        if (a1c === max) {answer.push(1)};
        if (a2c === max) {answer.push(2)};
        if (a3c === max) {answer.push(3)};
    
    
        return answer;
    }