OJ在线编程常见输入输出练习场
ACM模式下分V8和node.js
node.js=V8+内置基本模块,相当于java中的JRE=JVM+java标准库
node就是带有能操作IO和网络库的V8引擎,提供了很多可调用的API使得JavaScript能够读写文件,网络请求,系统信息等操作。对V8引擎进行了封装,执行JavaScript的速度更快,性能更好
不定行,定数目输入计算
//v8
while(line = readline()) {//不断读取整行
let lines = line.split(' ').map(Number);//字符串转字符数组再转成Number格式
let a = lines[0];
let b = lines[1];
console.log(a + b);
}
//node.js
let readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line) {
let lines = line.split(' ').map(Number);
console.log(lines[0] + lines[1]);
});
部分行参与计算+输入单个数字
//V8
let n = parseInt(readline());//读取一行,转数字
while(line = readline()) {//不断读入整行
let lines = line.split(' ').map(Number);
let a = parseInt(lines[0]);
let b = parseInt(lines[1]);
console.log(a + b);
}
//node.js
let readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let num = 0;
rl.on('line', function(line) {
if(num === 0) {
num = parseInt(line);
} else {
let [x, y] = line.split(' ').map(Number);
console.log(x + y);
}
});
终止输入判断(多个数字)
//V8
while(line = readline()) {
let lines = line.split(' ');
let a = parseInt(lines[0]);//可以用map将数组转数字,也可以每个数字单独转int
let b = parseInt(lines[1]);
if(a === 0 && b === 0) break;//终止判断
print(a + b);//输出可以用print也可以用console.log
}
//node.js
let readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line){
let [x, y] = line.split(' ').map(Number);
if(x === 0 && y === 0) return;
console.log(x + y);
})
终止输入判断(单个数字)
//V8
while(line = readline()) {
let lines = line.split(' ').map(Number);//转数字数组
if(lines[0] === 0) break;//终止判断
let res = lines.slice(1).reduce((sum, cur) => {
return sum + cur;
})
print(res);
}
//node.js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line) {
let arr = line.split(' ').map(Number);
if(arr[0] === 0) return;
console.log(arr.slice(1).reduce((sum, cur) => sum + cur));
})
定行,不定数目输入计算
//V8
let n = parseInt(readline());
while(n--) {//读取n行
let line = readline().split(' ').map(Number);
let res = line.slice(1).reduce((sum, cur) => {
return sum + cur;
})//计算从第二个数字开始到最后的和
print(res);
}
//node.js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let t = 0;
rl.on('line', function(line) {
if(t === 0) t = parseInt(line);
else {
let arr = line.split(' ').map(Number);
console.log(arr.slice(1).reduce((sum, cur) => sum + cur));
}
})
根据每行第一个数字决定该行需要计算的数量
//V8
while(line = readline()){
let lines = line.split(' ').map(Number);
let res = 0;
for(let i = 1;i <= lines[0]; i++){//也可以像上一题一样直接用slice + reduce
res += lines[i];
}
console.log(res);
}
//node.js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line) {
let arr = line.split(' ').map(Number);
console.log(arr.splice(1).reduce((sum, cur) => sum + cur));
})
不定行,不定数目输入计算
//V8
while(line = readline()) {
let lines = line.split(' ').map(Number);
let res = lines.reduce((sum, cur) => {
return sum + cur;
});
print(res);
}
//node.js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line) {
let arr = line.split(' ').map(Number);
console.log(arr.reduce((sum, cur) => sum + cur));
})
单行字符串排序输出
//V8
let n = parseInt(readline());
let lines = readline().split(' ');//转成数组后再用sort()方法
console.log(lines.sort().join(' '));
//node.js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let n = 0;
rl.on('line', function(line) {
if(n === 0) n = parseInt(line);
else {
let str = line.split(' ').sort().join(' ');
console.log(str);
}
})
多行字符串各自排序空格分隔输出
//V8
while(line = readline()) {//读取多行
let lines = line.split(' ');
console.log(lines.sort().join(' '));
}
//node.js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line) {
let str = line.split(' ').sort().join(' ');
console.log(str);
})
多行字符串各自排序逗号分隔输出
//V8
while(line = readline()) {
let lines = line.split(',');
let res = lines.sort().join(',');
print(res);
}
//node.js
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function(line) {
let str = line.split(',').sort().join(',');
console.log(str);
})