2023年10月26日之前的作业

This commit is contained in:
423A35C7 2023-10-26 14:24:53 +08:00
parent 8df48d1d8b
commit 0069fae271
17 changed files with 309 additions and 0 deletions

View File

@ -0,0 +1,40 @@
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

View File

@ -0,0 +1,40 @@
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 ]

View File

@ -0,0 +1,94 @@
const readline = require("readline");
const rl = readline.createInterface(process.stdin, process.stdout);
class Node {
previous;
value;
next;
}
class LinkList {
head;
length;
constructor(length) {
// 还是需要头结点此时head表示其中第0个结点length必须大于0
this.length = length;
this.head = new Node();
// this.head.value = 1;
let current, previous = this.head;
for (let i = 1; i <= this.length; i++) {
current = new Node();
current.value = i;
previous.next = current;
current.previous = previous;
previous = current;
}
current.next = this.head.next;
this.head.next.previous = current;
// this.head.next = this.current; // 不能多重赋值赋值语句会返回undefined
this.head.previous = current;
}
remove(node) {
node.previous.next = node.next;
node.next.previous = node.previous;
// delete node; // JavaScript里应该无法直接删除
this.length--;
return true;
}
// 找到从当前结点开始第n个结点可以为负数当前结点是第0个
// 并且再向后返回一个结点
get_n_after_node(node, n) {
let property_name = "next";
if (n < 0) {
property_name = "previous";
n = -n;
}
if (this.length < 3) return false;
for (let i = 0; i < n; i++) { // 向后走n个例如从0开始到3结束
node = node[property_name];
}
return node;
}
}
function main(people_num, num1) {
let link_list = new LinkList(people_num);
let current = link_list.head;
let temp;
while (true) {
temp = link_list.get_n_after_node(current, num1);
if (!temp) break;
current = temp;
link_list.remove(current);
}
// 此时current是最后一个被删掉的结点它的next和previous都是存在的结点
current = current.next;
link_list.head.next = current;
for (let i = 0; i < link_list.length; i++) {
console.log(current.value);
current = current.next;
}
}
function input(prompt) {
return new Promise((resolve) => {
rl.question(prompt, (answer) => {
resolve(answer);
});
});
}
async function main_() {
people_num = parseInt(await input("输入人数"));
num1 = parseInt(await input("输入第一个数字"));
main(people_num, num1);
rl.close();
}
main_()
// 输入人数41
// 输入第一个数字3
// 16
// 31

View File

@ -0,0 +1,135 @@
const readline = require("readline");
const rl = readline.createInterface(process.stdin, process.stdout);
class Node {
previous;
value;
next;
}
class LinkList {
head;
length;
constructor(length) {
// 还是需要头结点此时head表示其中第0个结点length必须大于0
this.length = length;
this.head = new Node();
// this.head.value = 1;
let current, previous = this.head;
for (let i = 1; i <= this.length; i++) {
current = new Node();
current.value = i;
previous.next = current;
current.previous = previous;
previous = current;
}
current.next = this.head.next;
this.head.next.previous = current;
// this.head.next = this.current; // 不能多重赋值赋值语句会返回undefined
this.head.previous = current;
}
remove(node) {
node.previous.next = node.next;
node.next.previous = node.previous;
// delete node; // JavaScript里应该无法直接删除
this.length--;
return true;
}
// 找到从当前结点开始第n个结点可以为负数当前结点是第0个
// 并且再向后返回一个结点
get_n_after_node(node, n) {
let property_name = "next";
if (n < 0) {
property_name = "previous";
n = -n;
}
if (this.length < 3) return false;
for (let i = 0; i < n; i++) { // 向后走n个例如从0开始到3结束
node = node[property_name];
}
return node;
}
}
function main(people_num, num1, num2) {
let link_list = new LinkList(people_num);
let current = link_list.head;
let temp;
while (true) {
temp = link_list.get_n_after_node(current, num1);
if (!temp) break;
current = temp;
link_list.remove(current);
temp = link_list.get_n_after_node(current, num2);
if (!temp) break;
current = temp;
link_list.remove(current);
}
// 此时current是最后一个被删掉的结点它的next和previous都是存在的结点
current = current.next;
link_list.head.next = current;
for (let i = 0; i < link_list.length; i++) {
console.log(current.value);
current = current.next;
}
}
function input(prompt) {
return new Promise((resolve) => {
rl.question(prompt, (answer) => {
resolve(answer);
});
});
}
function visualize(dryrun, link_list) {
if (dryrun) {
return;
}
let result = {
"kind": { "graph": true },
"nodes": [
],
"edges": [
]
};
let current = link_list.head.next;
for (let i = 0; i < link_list.length; i++) {
result.nodes.push({
"id": String(i),
"label": String(current.value)
});
current = current.next;
result.edges.push({
"from": String(i),
"to": String(i + 1)
});
}
result.edges[result.edges.length - 1].to = "0";
return result;
}
async function main_() {
visualize(true); // 不加这行可能它转化成Promise的时候不会放到闭包作用域里。
people_num = parseInt(await input("输入人数"));
num1 = parseInt(await input("输入第一个数字"));
num2 = parseInt(await input("输入第二个数字"));
main(people_num, num1, num2);
rl.close();
}
main_()
// 输入人数10000
// 输入第一个数字123
// 输入第二个数字-654
// 1299
// 4114
// 输入人数99999
// 输入第一个数字-848
// 输入第二个数字-351
// 80178
// 23680

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.