前言
不会用JS处理输入输出真的是浪费了春招笔试机会啊(ㄒoㄒ),以下只是给出示例处理,更多细节的处理要看题目要求我们取什么值。
JavaScript V8(6.0.0)
处理单行或固定行输入
输入例子1:
1 5
10 20
输出例子1:
1 5
10 20
function test(){
function test(){
let stringArr1 = readline().split(" ");
let stringArr2 = readline().split(" ");
print(stringArr1[0],stringArr1[1]);
print(stringArr2[0],stringArr2[1]);
}
test();
}
处理多行输入
输入包括两个正整数a,b(1 <= a,b <= 1000),输入数据包括多组。 输出a+b的结果。
输入例子2:
1 5
10 20
输出例子2:
6
30
function test(){
while(line = readline()){
let stringArr = line.split(" ");
let a = parseInt(stringArr[0]);
let b = parseInt(stringArr[1]);
print(a + b);
}
}
test();
JavaScript Node(12.18.2)
处理单行输入
输入例子3:
1 5
输出例子3:
1 5
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line',function(line){
var tokens = line.split(' ');
console.log(parseInt(tokens[0]), parseInt(tokens[1]));
});
处理固定行输入
输入例子4:
2
1 5
10 20
输出例子4:
6
30
function test(){
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let hang = -1;
let store = [];
rl.on('line',function(line){
if(hang == -1){
hang = parseInt(line[0]);
}
else{
store.push(line.split(" "));
if(hang == store.length){
console.log(parseInt(store[0][0]) + parseInt(store[0][1]));
console.log(parseInt(store[1][0]) + parseInt(store[1][1]));
}
}
});
}
test();
处理多行输入
输入例子5:
1 5
10 20
2 8
输出例子5:
6
30
10
function test(){
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let index = 0;
var store = [];
rl.on('line',function(line){
if(line.split(" ").length != 0){
store.push(line.split(" "));
console.log(parseInt(store[index][0]) + parseInt(store[index][1]));
index++;
}
});
}
test();
参考
【1】https://blog.csdn.net/helloworldpilipala/article/details/88375476 【2】https://www.cnblogs.com/FHC1994/p/12595912.html
|