我有一些学生的个人资料,其中包含物理,化学和数学等多个学科的价值。我需要根据各个学科的分数找到一份优势学生名单。例如:
let students = [{name: "A",phy: 70,chem: 80,math: 90},{name: "B",phy: 75,chem: 85,math: 60},{name: "C",phy: 78,chem: 81,math: 92},{name: "D",math: 55}];
如果一个学生满足以下两个条件,它将比另一个学生具有优势。 1.所有参数的student_1> = Student_2 2.学生_1>学生_2至少有一个参数
我尝试过使用嵌套循环。可能是蛮力算法。我添加了另一个名为“ passed”的参数来跟踪它是否比其他参数更占优势。这是代码:
let students = [{ name: "A",math: 90,passed: true },{ name: "B",math: 60,{ name: "C",math: 92,{ name: "D",math: 55,passed: true }];
let weak_student: any;
for (let student_1 of students) {
if (student_1.passed == false ||
students[students.length] === student_1) {
continue;
}
let compareList = students.filter(i => i.name != student_1.name && i.passed == true);
for (let student_2 of compareList) {
if ((student_1.phy >= student_2.phy &&
student_1.chem >= student_2.chem &&
student_1.math >= student_2.math)
&&
(student_1.phy > student_2.phy ||
student_1.chem > student_2.chem ||
student_1.math > student_2.math)
) {
weak_student = students.find(i => i.name === student_2.name);
weak_student.passed = false;
} else if (student_1.phy < student_2.phy &&
student_1.chem < student_2.chem &&
student_1.math < student_2.math) {
student_1.passed = false;
break;
}
}
}
console.log(students);
我发现预期的结果是学生A&D的标志“通过” ==否。现在,我需要使用不同的算法(例如“分而治之”,“最近的邻居”,“分支与边界”等)或任何其他有效方式来获得相同的结果。我需要比较大型数据集的时间和空间复杂度算法。