基于 node 进行概括的吧
单行输入
let readline = require('readline');
let r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const fn = function(传入参数) {
// 编辑程序
}
rl.on('line', function(line) {
const datas = line.split(' '); // 对输入以空格进行分割
fn(datas) // 调用函数fn
})
rl.on('close', function() {
process.exit(0)
})
多行输入
let readline = require('realine');
let rl = readline.creatInterface({
input: process.stdin,
output: process.stdout
})
const fn = function(输入参数) {
// 编程程序
}
var n = -1; // 默认总行为-1
var arr = []; // 用于装输入
var cur_line = 0; // 读取的行数
rl.on('line', function(line) {
if(n === -1) {
n = parseInt(line.trim()) ; // 取第一行的第一个数,一般第一行会表示接下来有多少行
}else {
let token = line.split(' ');
arr.push(token); // 将每行的输入都做成一个数组,arr 相当于是二维的数组;
cur_line++;
}
if(cue_line === n) {
// 调用 fn 函数,对数据进行操作;
}
})
有点懒了,后续再加吧~~~
多行输入
上面的那个只适用于知道输入的行数,下面这个适用于未知的输入行数
let readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// 用于接收输入的数组
const intArr = [ ];
//
rl.on('line', function (line) {
// 如果不加.map(Number) intArr里面的内容是字符串,加了 里面的内容是 数字
intArr.push(line.split(' ').map(Number))
}).on('close', function() {
要运行的核心代码
})