[Lv.1]模擬試験
39186 ワード
🔶 私の解決方法
は、
function solution(answers) {
const person = [
[1, 2, 3, 4, 5],
[2, 1, 2, 3, 2, 4, 2, 5],
[3, 3, 1, 1, 2, 2, 4, 4, 5, 5],
];
const correct = [[], [], []]; // [[1,2,3,4,5],[],[]];
for (let j = 0; j < person.length; j += 1) {
for (let i = 0; i < answers.length; i += 1) {
if (answers[i] === person[j][i % person[j].length]) {
correct[j].push(person[j][i % person[j].length]);
}
}
}
const rank = []; // [5, 0, 0] [2,2,2] [2,3,1]
for (let i = 0; i < correct.length; i += 1) {
rank.push(correct[i].length);
}
const maxNum = Math.max(...rank);
const result = [];
for (let i = 0; i < rank.length; i += 1) {
if (maxNum === rank[i]) {
result.push(i + 1);
}
}
return result;
}
🔶 他人を解く
方法.
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;
}
方法.function solution(answers) {
const man1 = [1, 2, 3, 4, 5];
const man2 = [2, 1, 2, 3, 2, 4, 2, 5];
const man3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
const count = [0, 0, 0];
const result = [];
for (let i = 0; i < answers.length; i += 1) {
if (answers[i] === man1[i % man1.length]) count[0] += 1;
if (answers[i] === man2[i % man2.length]) count[1] += 1;
if (answers[i] === man3[i % man3.length]) count[2] += 1;
}
const max = Math.max(count[0], count[1], count[2]);
for (let i = 0; i < count.length; i += 1) {
if (max === count[i]) {
result.push(i + 1);
}
}
return result;
}
方法.function solution(answers) {
const man1 = [1, 2, 3, 4, 5];
const man2 = [2, 1, 2, 3, 2, 4, 2, 5];
const man3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
const rank = [0, 0, 0];
const result = [];
for (let i = 0; i < answers.length; i += 1) {
if (answers[i] === man1[i % man1.length]) {
rank[0] += 1;
}
}
for (let i = 0; i < answers.length; i += 1) {
if (answers[i] === man2[i % man2.length]) {
rank[1] += 1;
}
}
for (let i = 0; i < answers.length; i += 1) {
if (answers[i] === man3[i % man3.length]) {
rank[2] += 1;
}
}
const max = Math.max(...rank);
for (let i = 0; i < rank.length; i += 1) {
if (max === rank[i]) {
result.push(i + 1);
}
}
return result;
}
🔶フィードバック
a%b:aがbより小さい場合、aをbで割ることはできないので、aは残ります.
console.log(1 % 8); // 1
console.log(2 % 8); // 2
console.log(3 % 8); // 3
console.log(4 % 8); // 4
console.log(5 % 8); // 5
console.log(6 % 8); // 6
console.log(7 % 8); // 7
// 7을 8로 못나눠서, 7이 그대로 나머지로 나옴
console.log(8 % 8); // 0
console.log(9 % 8); // 1
console.log(10 % 8); // 2
console.log(11 % 8); // 3
console.log(12 % 8); // 4
console.log(13 % 8); // 5
console.log(14 % 8); // 6
console.log(15 % 8); // 7
console.log(16 % 8); // 0
console.log(17 % 8); // 1
console.log(18 % 8); // 2
Reference
この問題について([Lv.1]模擬試験), 我々は、より多くの情報をここで見つけました https://velog.io/@jhplus13/Lv.1모의고사テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol