SharedSchoolSpace/作业/数据结构-金健/JavaScript/第一章作业1.2.js

41 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-10-26 14:24:53 +08:00
const readline = require("readline");
const rl = readline.createInterface(
process.stdin, process.stdout
);
let student_num = 10;
let grades = new Array(student_num); // 类似于C/C++中数组的数据结构空间复杂度O(n)
function input(remain_times) {
if (remain_times <= 0) {
rl.close();
return;
}
rl.question("", (answer) => {
grades[student_num - remain_times] = parseFloat(answer); // 暂时不考虑输入错误
input(remain_times - 1);
});
}
rl.on("close", () => {
let sum = grades.reduce((sum, current) => {
return sum + current;
}, 0); // 时间复杂度O(n)
let average = sum / student_num;
console.log("高于平均分的学生成绩为:", grades.filter((grade) => grade > average)); // 时间复杂度O(n)
});
rl.write("输入"+student_num+"名学生的成绩,一个一行:\n");
input(student_num);
// 输入10名学生的成绩一个一行
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
// 高于平均分的学生成绩为: [ 6, 7, 8, 9, 10 ]