目录
1.计算多组a+b(组数不定)
![](https://i-blog.csdnimg.cn/blog_migrate/31d2ca9a9888e44e5eeeed459ab18da4.png#pic_center)
V8
while (line = readline()) {
var lines = line.split(' ');
var a = parseInt(lines[0]);
var b = parseInt(lines[1]);
print(a + b);
}
Node
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let inputArr = [];
rl.on("line", function (input) {
let res = input.split(' ').map(i=>+i); // 获取此行数据
console.log(res[0]+res[1])
});
2.计算多组a+b(组数确定)
![](https://i-blog.csdnimg.cn/blog_migrate/d4fd5e69734fea734e295a6528dd582c.png#pic_center)
let number = parseInt(readline());
while(number--){
let line =readline();
let arr = line.split(' ');
let a = parseInt(arr[0]);
let b = parseInt(arr[1]);
print(a+b);
}
Node
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let inputArr = [];
rl.on("line", function (input) {
inputArr.push(input.split(' ').map(Number)); // 获取此行数据
const nLine = parseInt(inputArr[0]); // 获取行数
if (inputArr.length == nLine+ 1) {
// 获取了此轮所有数据,此时获取元素 nLine+1 个,第一个元素为行数nLine.
const arr = inputArr.slice(1); // 除去行数的具体数据
arr.forEach(item=>{
console.log(item[0]+item[1])
})
inputArr = []; // 清空数组
rl.close();
}
});
3.计算多组a+b(0 0结束输入)
![](https://i-blog.csdnimg.cn/blog_migrate/19915819215bf555a29801d54ae854ae.png#pic_center)
V8
let line
while(line = readline()){
let arr = line.split(' ').map(item=>parseInt(item))
if(arr[0]!==0 && arr[1]!==0){
print(arr[0]+arr[1])
}
}
Node
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let inputArr = [];
rl.on("line", function (input) {
let curinput=input.split(' ').map(Number)
inputArr.push(curinput); // 获取此行数据
const nLine = parseInt(inputArr[0]); // 获取行数
if(curinput[0]==0 && curinput[1]==0){
let arr= inputArr.slice(0,-1)
arr.forEach(item=>{
console.log(item[0]+item[1])
})
}
});
4.计算一系列数的和(组数不定,n为0结束输出)
V8
while(line = readline())
{
let arr = line.split(' ').map(item=>parseInt(item))
if(arr[0]!==0){
let res = 0
for(i = 1;i<arr.length;i++){
res+=arr[i]
}
print(res)
}
}
Node
const readline = require("readline");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let inputArr1 = [];
rl.on("line", function (input) {
let curinput =input.split(' ').map(Number)
inputArr1.push(curinput); // 获取此行数据
// const nLine = parseInt(inputArr[0]); // 获取行数
function sum(arr){
return arr.reduce((acc,cur)=>acc+cur)
}
if (curinput.length==1 && curinput[0]==0) {
// 获取了此轮所有数据,此时获取元素 nLine+1 个,第一个元素为行数nLine.
const arr = inputArr1.slice(0,-1); // 除去行数的具体数据
arr.forEach(item=>{
console.log(sum(item.slice(1)))
})
inputArr1 = []; // 清空数组
rl.close();
}
});
5.计算一系列数的和(组数确定)
![](https://i-blog.csdnimg.cn/blog_migrate/0e1de2401b1f542c912b77466e910953.png#pic_center)
V8
let number = parseInt(readline());
while(number--){
let line = readline();
let arr = line.split(' ').map(item=>parseInt(item));
let sum = 0;
for(let i = 1; i <= arr[0]; i++) {
sum += arr[i];
}
print(sum);
}
Node
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output:process.stdout
});
const arr = [];
let n=-1;
let curLine =0;
rl.on('line', function(line){
if(n<0){
n = parseInt(line);
}else{
arr.push(line.split(' ').map(Number))
curLine++;
}
if(curLine === n){
for(let i=0; i<arr.length; ++i){
let ans =0;
for(let j =1; j<arr[i].length; ++j){
ans+= arr[i][j];
}
console.log(ans)
}
}
})
6.计算一系列数的和(组数不定)
![](https://i-blog.csdnimg.cn/blog_migrate/a698a7876fecb6c36adde2ded2600767.png)
V8
while(line = readline())
{
let arr = line.split(' ').map(item=>parseInt(item))
let res = 0
for(i = 1;i<arr.length;i++){
res+=arr[i]
}
print(res)
}
Node
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);
//每行含义都相同时不需要push
//含义不同,有明确的停止条件时才用数组
let ans =0;
for(let i=1; i<arr.length;++i){
ans+= arr[i];
}
console.log(ans)
})
7.计算一系列数的和(每行数不定)
![](https://i-blog.csdnimg.cn/blog_migrate/ce6fd35f19959a9aa94de0aa77d8c2aa.png#pic_center)
V8
while(line = readline())
{
let arr = line.split(' ').map(item=>parseInt(item))
let res = 0
for(i = 0;i<arr.length;i++){
res+=arr[i]
}
print(res)
}
Node
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);
//每行含义都相同时不需要push
//含义不同,有明确的停止条件时才用数组
let ans =0;
for(let i=0; i<arr.length;++i){
ans+= arr[i];
}
console.log(ans)
})
8.对输入的字符串进行排序后输出
![](https://i-blog.csdnimg.cn/blog_migrate/d8fa981fd27246f7d3f4ca77e62a8f3d.png#pic_center)
V8
Node
9.对输入的字符串进行排序后输出(多组)
![](https://i-blog.csdnimg.cn/blog_migrate/ae5a701d8522109953a9e09754d19e8d.png#pic_center)
V8
while(line=readline()){
let arr=line.split(' ')
print(arr.sort().join(' '))
}
Node
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (line) {
let arr = line.split(' ').sort();
console.log(arr.join(' '));
});
10.字符串排序(3)
![](https://i-blog.csdnimg.cn/blog_migrate/9b26a5278313bb9cc622ce504f56d73c.png#pic_center)
V8
while(line=readline()){
if(line !== 'nowcoder') {
let arr=line.split(',')
print(arr.sort().join(','))
}
}
Node
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (line) {
if(line !== 'nowcoder'){
let arr = line.split(',').sort();
console.log(arr.join(','));
}
});
11.两数之和
![](https://i-blog.csdnimg.cn/blog_migrate/6068a38f8afbaed9d9231ff88d47e0f3.png#pic_center)
![](https://i-blog.csdnimg.cn/blog_migrate/5949491bbb1dcd7041342691cde8454c.png#pic_center)
v8
while(line = readline()){
var lines = line.split(' ').map(Number)
console.log(lines[0] + lines[1])
}
V8
while(line=readline()){
let arr=line.split(' ')
print(Number(arr[0])+Number(arr[1]))
}
Node
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[0]+arr[1])
})