15选择,两道编程。
第一个编程 判断字符串包含red且不包含der。
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let have = 'red'
let not = 'der'
let inputs = []
rl.on('line', function (line) {
inputs.push(line)
if(Number(inputs[0]) === inputs.length-1) {
let num = Number(inputs[0])
for (let i = 1; i < inputs.length; i++) {
if(inputs[i].includes(have) && !inputs[i].includes(not)) {
console.log('Yes')
} else {
console.log('No')
}
}
process.exit(0)
}
});
第二个是 输入一个数组 在只对该数组的相邻两个数交换至多一次的情况下,求这个数组的所有相邻元素的差的绝对值之和。
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let inputs = []
let sum = 0
let max = 0
rl.on('line', function (line) {
inputs.push(line)
if(inputs.length === 2) {
let arr = inputs[1].split(' ').reduce(function(pre,cur){
return pre.concat(Number(cur))
},[])
// console.log(arr)
function exachange(arr,i,j) {
[arr[i],arr[j]] = [arr[j],arr[i]]
}
let newArr = arr.slice(0)
for (let i = 0; i < arr.length - 1; i++) {
exachange(arr,i,i+1)
for (let index = 0; index < arr.length - 1; index++) {
let re = Math.abs(arr[index]-arr[index+1])
sum += re
}
max = Math.max(max,sum)
sum = 0
console.log(arr)
arr = newArr.slice(0)
}
console.log(max)
process.exit(0)
}
});
#面经##秋招#