以下是某班级一次考试的成绩表。请计算每个学生总成绩,并按总成绩排名。找出各单科成绩第一名,输出其成绩与学号
var scoreArr = [{
sno: 1,
chinese: 105,
math: 62,
english: 118
},
{
sno: 2,
chinese: 89,
math: 78,
english: 120
},
{
sno: 3,
chinese: 86,
math: 64,
english: 80
},
{
sno: 4,
chinese: 78,
math: 99,
english: 91
},
{
sno: 5,
chinese: 107.5,
math: 97,
english: 70
},
{
sno: 6,
chinese: 112,
math: 61,
english: 92
},
{
sno: 7,
chinese: 101,
math: 79,
english: 104
},
{
sno: 8,
chinese: 71,
math: 72,
english: 105
},
{
sno: 9,
chinese: 56,
math: 68,
english: 61
},
{
sno: 10,
chinese: 98,
math: 83,
english: 77
}]
const scoreArr = [
{
sno: 1,
chinese: 105,
math: 62,
english: 118,
},
{
sno: 2,
chinese: 89,
math: 78,
english: 120,
},
{
sno: 3,
chinese: 86,
math: 64,
english: 80,
},
{
sno: 4,
chinese: 78,
math: 99,
english: 91,
},
{
sno: 5,
chinese: 107.5,
math: 97,
english: 70,
},
{
sno: 6,
chinese: 112,
math: 61,
english: 92,
},
{
sno: 7,
chinese: 101,
math: 79,
english: 104,
},
{
sno: 8,
chinese: 71,
math: 72,
english: 105,
},
{
sno: 9,
chinese: 56,
math: 68,
english: 61,
},
{
sno: 10,
chinese: 98,
math: 83,
english: 77,
},
];
let sum,
maxChinese = 0,
maxChineseSno,
maxMath = 0,
maxMathSno,
maxEnglish = 0,
maxEnglishSno;
// 快速排序时间复杂度O(nlogn)空间复杂度O(1)
function quickSort(arr) {
if (arr.length <= 1) return arr;
const center = Math.trunc(arr.length / 2);
const temp = arr.splice(center, 1);
let left = [];
let right = [];
const length = arr.length;
const temp2 = temp[0].chinese + temp[0].math + temp[0].english;
for (let i = 0; i < length; i++) {
const sum = arr[i].chinese + arr[i].math + arr[i].english;
if (sum < temp2) left.push(arr[i]);
else right.push(arr[i]);
}
const newArr = quickSort(left).concat(temp, quickSort(right));
return newArr;
}
console.log(quickSort(scoreArr));
// for循环时间复杂度O(n)
for (let i = 0; i < scoreArr.length; i++) {
sum = scoreArr[i].chinese + scoreArr[i].math + scoreArr[i].english;
if (scoreArr[i].chinese > maxChinese) {
maxChinese = scoreArr[i].chinese;
maxChineseSno = scoreArr[i].sno;
}
if (scoreArr[i].math > maxMath) {
maxMath = scoreArr[i].math;
maxMathSno = scoreArr[i].sno;
}
if (scoreArr[i].english > maxEnglish) {
maxEnglish = scoreArr[i].english;
maxEnglishSno = scoreArr[i].sno;
}
}
console.log('\n' + '语文单科成绩最高的是' + maxChinese + '学号为:' + maxChineseSno);
console.log('数学单科成绩最高的是' + maxMath + '学号为:' + maxMathSno);
console.log('英语单科成绩最高的是' + maxEnglish + '学号为:' + maxEnglishSno);