题解 | #记负均正#
记负均正
http://www.nowcoder.com/practice/6abde6ffcc354ea1a8333836bd6876b8
let numStr, inputCount = 0;
while (line = readline()) {
inputCount++;
if (inputCount % 2 === 0) {// 判断 第几次输入,2整除的话是第二次输入
let arr = line.split(' ').map(item => parseInt(item));
output(parseInt(numStr), arr)
} else {
numStr = line
}
}
function output(num, arr) {
let len = arr.length;
let sum = 0.0;
let avage = sum;
let count = 0;// 正整数的个数
arr.map((item, index) => {
if (item > 0) {
sum += item
count++
}
// 如果没有正整数
if (index === arr.length - 1 && count !== 0) {
avage = sum/count
}
})
let fushuArr = arr.filter(item => item < 0)// 负整数数组
console.log(fushuArr.length, avage.toFixed(1));
}
// 测试用例
// 5
// -1 -2 3 4 5