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

41 lines
949 B
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("平均分为:", average);
});
rl.write("输入"+student_num+"名学生的成绩,一个一行:\n");
input(student_num);
// 输入10名学生的成绩一个一行
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
// 平均分为: 5.5