输入 n 个整型数,统计其中的负数个数并求所有非负数的平均值,结果保留一位小数,如果没有非负数,则平均值为0
本题有多组输入数据,输入到文件末尾。
数据范围: ,其中每个数都满足
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 len = 0,len2 = 0; let count = 0 let average = 0 while(line = parseInt(await readline())){ if(line <0){ len +=1 }else{ len2 +=1 count +=line } } if(len2 == 0){ average = '0.0' }else{ average = (count/len2).toFixed(1) } console.log(len+ '\n' + average ) }()
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const lines = []; rl.on("line", function (line) { lines.push(parseInt(line)); }); rl.on("close", function () { let i = 0; let a = 0; lines.forEach((item) => { if (item < 0) { i++; } else { a += item; } }); console.log(i); if (lines.length === i) { console.log('0.0'); } else { console.log((a / (lines.length - i)).toFixed(1)); } });
function f5(arr){ let minus = 0 // 负数 let positive = [] // 正数 for (let i = 0 ; i<arr.length ;i++) { if(arr[i]){ if(arr[i]<0){ minus++ }else{ positive.push(arr[i]) } } } let length = positive.length let total = 0 for (let i in positive){ total+= Number(positive[i]) } let mean = total/length console.log(minus); console.log(mean.toFixed(1)) } while(line=readline()){ var lines = line.split(' '); f5(lines) }
var readline = require('readline'); var ri = readline.createInterface({ input: process.stdin, output: process.stdout }); var arr2=[]; var cout=0,sum=0,count=0; ri.on("line",function(line){ line=line.trim(); var arr2=line.split(' '); for(var i=0;i<arr2.length;i++){ if(arr2[i]<0){ cout++; }else{ count++; sum+=parseInt(arr2[i]); } } var ppp=sum/count; console.log(cout); console.log(ppp.toFixed(1)); })