题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
// const rl = require("readline").createInterface({ input: process.stdin });
// var iter = rl[Symbol.asyncIterator]();
// const readline = async () => (await iter.next()).value;
// void (async function () {
// // Write your code here
// let arrinput = [];
// while ((line = await readline())) {
// arrinput.push(line);
// }
// arrnum = arrinput[1].split(" ");
// // console.log(arrnum);
// // 创建3个数组,分别存入5的倍数、3的倍数、其余数
// let arr5 = [];
// let arr3 = [];
// let arrother = [];
// for (let item of arrnum) {
// // console.log(item%5==false)
// switch (true) {
// case item % 5 == 0: //能被5整除,返回true,因此,会执行下面的代码
// arr5.push(item);
// break;
// case item % 3 == 0: //能被3整除,返回true,因此,会执行下面的代码
// arr3.push(item);
// break;
// default:
// //上面的条件都不满足,说明此数不是5和3的倍数,因此,会执行下面的代码
// arrother.push(item);
// break;
// }
// }
// // 分别求得arr5、arr3的和
// let sum5 = 0;
// let sum3 = 0;
// // for (let item of arr5) {
// // if (item != undefined) {
// // // 如果非空,那么执行累加
// // sum5 += +item;
// // }
// // }
// // for (let item of arr3) {
// // if (item != undefined) {
// // // 如果非空,那么执行累加
// // sum3 += +item;
// // }
// // }
// // // 接着求出arrother中项的绝对值的和
// // let sumother = 0;
// // for (let item of arrother) {
// // if (item != undefined) {
// // // 如果非空,那么执行累加
// // sumother += Math.abs(item);
// // }
// // }
// for (let item of arr5) {
// sum5 += +item;
// }
// for (let item of arr3) {
// sum3 += +item;
// }
// // 接着求出arrother中项的相减的绝对值
// let sumother = 0;
// // 对除过第一项之外的数字,取反
// for (let i = 1; i < arrother.length; i++) {
// arrother[i] = -+arrother[i];
// }
// // 然后挨个求和
// for (let item of arrother) {
// sumother += +item;
// }
// // console.log(sum5, sum3, sumother);
// // 判断,如果abs(sum5-sum3)==abs(sumother),如果相等,那么可以匹配
// if (Math.abs(sum5 - sum3) == Math.abs(sumother)) {
// console.log(true);
// } else {
// console.log(false);
// }
// })();
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
// Write your code here
let arrinput = [];
while ((line = await readline())) {
arrinput.push(line);
}
arrnum = arrinput[1].split(" ");
// console.log(arrnum);
// 创建3个数组,分别存入5的倍数、3的倍数、其余数
let arr5 = [];
let arr3 = [];
let arrother = [];
for (let item of arrnum) {
// console.log(item%5==false)
switch (true) {
case item % 5 == 0: //能被5整除,返回true,因此,会执行下面的代码
arr5.push(+item);
break;
case item % 3 == 0: //能被3整除,返回true,因此,会执行下面的代码
arr3.push(+item);
break;
default:
//上面的条件都不满足,说明此数不是5和3的倍数,因此,会执行下面的代码
arrother.push(+item);
break;
}
}
// 求和函数
function sum(arr) {
let sum = 0;
for (let item of arr) {
sum += +item;
}
return sum;
}
// 定义回溯函数,挨个分配
function fun(arr3, arr5, arrother) {
let result = false;
function backTracking(arr3, arr5, n) {
if (result == true) {
//终止条件
return;
} else if (arrother.length == 0) {
if (sum(arr3) == sum(arr5)) {
result = true;
return;
} else {
return;
}
}
for (let i = 1; i <= n; i++) {
let m = arrother.pop();
if (i == 1) {
arr5.push(m);
} else {
arr3.push(m);
}
backTracking(arr3, arr5, n);
arrother.push(m);
if (i == 1) {
arr5.pop();
} else {
arr3.pop();
}
}
}
backTracking(arr3, arr5, 2);
return result;
}
console.log(fun(arr3, arr5, arrother));
})();
查看11道真题和解析