题解 | #数组分组#
数组分组
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 num = Number(await readline())
let arr = (await readline()).split(' ').map(Number)
//构建5的倍数、3的倍数的数组arr5和arr3,及剩余数字组成的数组arr
let arr5 = [], arr3 = []
for(let i=0; i<arr.length; i++){
if(arr[i]%5 == 0){
arr5.push(arr[i])
arr.splice(i, 1)
i--
}else if(arr[i]%3 == 0){
arr3.push(arr[i])
arr.splice(i, 1)
i--
}
}
//将arr5和arr3的所有数值加和求出sum5和sum3
let sum5=0, sum3=0
if(arr5.length>0){
for(let k of arr5){
sum5 += k
}
}
if(arr3.length>0){
for(let k of arr3){
sum3 += k
}
}
//将sum5和sum3的差值和arr数组中的值放在同一数组中,并将将每个数字转化成正整数
let newArr = [sum3-sum5, ...arr].map((n) => Math.abs(n))
newArr.sort((a,b) => b-a)
while(newArr.length>1){
let big = newArr.shift()
let small = newArr.shift()
newArr.push(big-small)
newArr.sort((a,b) => b-a)
}
console.log(newArr[0]==0)
}()