输入 n 个整型数,统计其中的负数个数并求所有非负数的平均值,结果保留一位小数,如果没有非负数,则平均值为0
本题有多组输入数据,输入到文件末尾。
数据范围: ,其中每个数都满足
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int cntn = 0, cntp = 0;
float sum = 0;
while(sc.hasNext()){
int num = sc.nextInt();
if(num < 0) cntn++;
else{
sum += num;
cntp++;
}
}
DecimalFormat fnum = new DecimalFormat("##0.0");
System.out.println(cntn);
if(cntp == 0) System.out.println("0.0");
else System.out.println(fnum.format(sum / cntp));
}
}
#include <iostream> #include <iomanip> using namespace std; int main() { int m; int s=0; int ct=0; int zct=0; while(cin>>m) { if (m<0) { ++ct; } else if(m>0) { s+=m; ++zct; } } double v=(double)s/zct; cout<<ct<<endl; cout<<setiosflags(ios::fixed)<<setprecision(1)<<v<<endl; return 0; }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int count1 = 0; int count2 = 0; float sum = 0; while(in.hasNext()){ int n = in.nextInt(); if(n<0){ count1++; }else{ count2++; sum+=n; } } float average = sum/count2; System.out.println(count1); System.out.printf("%.1f\n",average); } }
#include<iostream> #include<vector> using namespace std; int main() { int n; int count1 = 0; int count2 = 0; double arv = 0; while(cin>>n) { if(n>=0) { count1++; arv += n; } else { count2++; } } printf("%d\n%0.1lf\n",count2,arv/count1); return 0; }没有太多好说的,很基础的题了,就是后面的输出自己要注意点。而且他是循环输入的,一定要注意
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //非负数数和 int pos = 0; //非负数个数 int posCount = 0; //负数个属于 int negCount = 0; while(sc.hasNextInt()){ int n = sc.nextInt(); if(n >= 0){ pos += n; posCount ++ ; } if(n < 0){ negCount++; } } System.out.println(negCount); if(posCount==0){ System.out.println(0.0); }else{ System.out.printf("%.1f",(double)pos/posCount); } } }
...... int countfu = 0; int countsum = 0; float countzeng = 0; ...... float f = countsum/countzeng; ......
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int countF=0,countZ=0,sum=0; while(sc.hasNext()){ int n=sc.nextInt(); if(n<0){ countF++; } else if(n>0){ countZ++; sum+=n; } } System.out.println(countF); if(countZ==0) System.out.println(0.0); else System.out.println(String.format("%.1f",(double)sum/countZ)); } }
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) }
while True: try: number_list = list(map(int, input().split())) negative_count = 0 non_negative_count = 0 non_negative_sum = 0 for i in number_list: if i < 0: negative_count += 1 else: non_negative_count +=1 non_negative_sum += i print(negative_count) print(round(non_negative_sum / non_negative_count, 1)) except: break
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int micnt = 0; int zhcnt = 0; int zhsum = 0; while(sc.hasNext()) { int n = sc.nextInt(); if(n < 0) { micnt ++; } else { zhcnt ++; zhsum += n; } } System.out.println(micnt); if(zhcnt == 0) { zhcnt = 1; } System.out.format("%.1f",zhsum*1.0/zhcnt); } }