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

41 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 ]