首页 > 试题广场 >

记负均正

[编程题]记负均正
  • 热度指数:283628 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的 n 个整数 a_1, a_2, \cdots, a_n,统计并计算:
\hspace{23pt}\bullet\,负整数的个数;
\hspace{23pt}\bullet\,正整数的平均值。

输入描述:
\hspace{15pt}第一行输入一个整数 n \left(1 \leqq n \leqq 2 \times 10^3\right) 代表整数的个数。
\hspace{15pt}第二行输入 n 个整数 a_1, a_2, \dots, a_n \left(-10^3 \leqq a_i \leqq 10^3\right) 代表输入的整数。


输出描述:
\hspace{15pt}先输出一个整数,代表负整数的个数;随后在同一行输出一个实数,代表正整数的平均值。

\hspace{15pt}由于实数的计算存在误差,当误差的量级不超过 10^{-6} 时,您的答案都将被接受。具体来说,设您的答案为 a ,标准答案为 b ,当且仅当 \tfrac{|a-b|}{\max(1,|b|)}\leqq 10^{-6} 时,您的答案将被接受。
示例1

输入

6
-1 3 -2 1 0 1

输出

2 1.66666666667
示例2

输入

3
0 0 0

输出

0 0

备注:
\hspace{15pt}本题输出要求已规范,允许细微误差(2025/01/16)。
import java.util.*;

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        int count1=0;
        int sum = 0;
        int count2=0;
        int count3 = 0;
        for(int i=0;i<a;i++){
            int b = in.nextInt();  
            if(b<0){
                count1++;
            }else if(b>0){
                sum+=b;
                count2++;
            }else{
                count3++;
            }
        }
        double avg = (double)sum/count2;
       if(count3 == a-count1){
            System.out.println(count1+ " " + 0);
       }else{
            System.out.print(count1+ " "+ String.format("%.11f",avg));
       }
    }
}

发表于 2025-05-22 19:45:24 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int count = 0;
        int o = 0;
        double x = 0;
        double num = 0;
        in.nextLine();

        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            if (a < 0) {
                count++;
            } else if (a > 0) {
                num = num + a;
                x++;
            } else{
                o++;
            }
        }
        double p =num/x;
        double q = p*Math.pow(10,10);
        Math.round(q);
        q = q/(Math.pow(10,10));
        //String q = String.format("%.11f", p);不使用String.format()方法 使用数学方法 取四舍五入以及10的n次方扩大 后四舍五入 缩小 输出
        if(o==n-count){
            q = 0;
        }

        System.out.print(count + " " + q);

    }
}
发表于 2025-03-16 13:33:11 回复(0)
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        int count=0;
        int sum = 0;
        int count2=0;
        for(int i=0;i<a;i++){
            int b = in.nextInt();  
            if(b<0){
                count++;
            }else if(b>0){
                sum+=b;
                count2++;
            }
        }
        double avg =0.0;
        if(count2!=0){
            avg = (double)sum/count2;
        }
         System.out.print(count+" "+String.format("%.1f",avg));
    }
}

算法很简单,但是难点在于两个点:
1、输出为保留一位小数的值,需要使用String.format("%.1f",avg) 进行转换
2、两个整数相除怎么能有小数,如果这么写 (double)(sum/count2),结果小数位一直为0,需要将sum转为double型才行,即 (double)sum/count2
3、需要处理被除数为0 的情况

发表于 2023-09-05 18:34:34 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
             int a = in.nextInt();
            int sum = 0;
            int positiveNumber = 0;
            int negativeNumber = 0;
            for(int i= 0; i < a;i++){
                int n = in.nextInt();
                if(n > 0 && n != 0){
                    sum += n;
                    positiveNumber ++;
                }else if (n < 0){
                    negativeNumber++;
                }
            }
            double dou = 0;
            if (positiveNumber != 0) {
                 dou = (double) sum/positiveNumber;
            }
            System.out.print(negativeNumber+" "+String.format("%.1f",dou));
        }
    }
}

发表于 2023-06-08 16:49:02 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        ArrayList<Integer> list=new ArrayList<>();
        int n=in.nextInt();
        int sum=0;
        for(int i=0;i<n;i++){
            Integer tmp=Integer.valueOf(in.next());
            if(tmp<0){
                sum++;
            }else if(tmp>0){
                list.add(tmp);
            }
        }
        Double result=0.0;
        for(int i=0;i<list.size();i++){
            result+=list.get(i);
        }
        if(list.size()==0){
            System.out.print(sum+" "+0.0);
        }else{
            result=result/list.size();
            System.out.print(sum+" "+String.format("%.1f",result));
        }
        
    }
}

发表于 2023-06-07 09:37:02 回复(0)
 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int zCount = 0;
            int zNum = 0;
            int fNum = 0;
            for(int i=0;i<n;i++){
                int num = in.nextInt();
                if(num>0){
                    zNum++;
                    zCount+=num;
                }else if(num<0){
                    fNum++;
                }
            }
           
            double dd = zNum==0?0.0:Math.round((double)(zCount*10)/(double)zNum)/10.0;//要把两个相除的数用(double)包起来, 否则丧失精度;保存两位小数用这个DecimalFormat df =new DecimalFormat("#####0.00");df.format(c);
            System.out.print(fNum + " " +dd);
        }
    }
发表于 2023-04-20 21:13:07 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while((str=br.readLine())!=null){
            int cou=Integer.parseInt(str);
            int[] dd=new int[cou];
            String[] strArr=br.readLine().split(" ");
            for(int i=0;i<cou;i++){
                dd[i]=Integer.parseInt(strArr[i]);
            }
            int fu=0;
            double sum=0.0;
            int lin=0;
            for(int i=0;i<cou;i++){
                if(dd[i]<0){
                    fu++;
                    continue;
                }else if(dd[i]==0){
                    lin++;
                    continue;
                }
                sum+=dd[i];
            }
            if(cou-fu-lin==0){
                System.out.println(fu+" "+"0.0");
            }else{
                double s=sum/(cou-fu-lin);
//                
                
                
                System.out.println(fu+" "+String.format("%.1f",s));
            }
            
        }
    }
}

发表于 2022-08-02 14:42:54 回复(0)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int length = scanner.nextInt();
		int[] ar = new int[length];
		for (int i = 0; i < length; i++) {
			ar[i] = scanner.nextInt();
		}
		int counts_=0;
		int counts=0;int sum=0;
		for (int i = 0; i < length; i++)
		{
			if(ar[i]>0) {sum+=ar[i];counts+=1;}
			if(ar[i]<0) counts_+=1;
		}
		double avg= Math.round(sum*1.0/counts*10)*1.0/10;
		System.out.println(counts_+" "+avg);
		
	}
}

发表于 2022-07-26 05:25:17 回复(0)
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer> list = new ArrayList();
        while (sc.hasNext()) {
            list.add(sc.nextInt());
        }
        List<Integer> negative = list.stream().filter(x->x < 0)
                                 .collect(Collectors.toList());
        int count = negative.size();
        List<Integer> positive = list.stream().filter(x->x > 0)
                                 .collect(Collectors.toList());
        long sum = positive.stream().mapToInt(x->x).sum();
        double size = positive.size();
        double average = 0.0d;
        if (size > 0) {
            average = sum / size;
        }
        System.out.println(String.format("%d %.1f", count, average));
    }
}
发表于 2022-07-10 12:44:31 回复(0)
import java.util.*;

// 注意:正数平均值如果都为0,字符串转换是NAN,需要提前输出,避免都为0还执行,见用例2
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int nextInt = scanner.nextInt();
        int fushu = 0;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nextInt; i++) {
            String next = scanner.next();
            int num = Integer.parseInt(next);
            if (num == 0)
                continue;
            if (num < 0) {
                ++fushu;
            } else {
                list.add(num);
            }
        }
        System.out.print(fushu + " ");
        int size = list.size();
        int sum = 0;
        if (size == 0) {
            System.out.println(0.0);
        } else {
            for (Integer integer : list) {
                sum += integer;
            }
             System.out.println(String.format("%.1f", sum * 1.0 / size));
        }
        
    }
}
发表于 2022-06-16 14:52:30 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int[] ints = new int[n];
            for (int i = 0; i < ints.length; i++) {
                ints[i] = sc.nextInt();
            }
            solution(ints);
        }
    }

    private static void solution(int[] ints) {
        double avg = 0.0;
        int countB = 0;
        int countS = 0;
        for (int anInt : ints) {
            if (anInt > 0) {
                avg += anInt;
                countB++;
            } else if (anInt < 0) {
                countS++;
            }
        }
        if (countB > 0) {
            avg /= countB;
        }
        String avg1 = String.format("%.1f", avg);
        System.out.println(countS + " " + avg1);
    }

}

发表于 2022-03-28 13:34:31 回复(0)
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int s1 = 0,s2 = 0;
            double sum = 0.0;
            int[] arr = new int[n];
            for(int i = 0;i<n;i++){
                arr[i] = sc.nextInt();
            }
            for(int i = 0;i<arr.length;i++){
                if(arr[i] <0)
                    s1++;
                if(arr[i] >0){
                    sum= sum+arr[i];
                    s2++;
                }
            }
                if(sum==0){
                    System.out.print(s1+" "+"0.0"+"\n");
                }else if(sum!=0){
                    System.out.print(s1+" "+String.format("%.1f",sum/s2)+"\n");
                }
        }
    }

发表于 2022-03-12 23:49:20 回复(0)
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int countFu = 0;
            int countZh = 0;
            double sum = 0.0;
            for (int i = 0; i < n; i++) {
                int in = sc.nextInt();
                if (in < 0) {
                    countFu++;
                }
                if (in > 0) {
                    countZh++;
                    sum = sum + in;
                }
            }
            System.out.println(countFu + " " + (countZh > 0? new DecimalFormat("######0.0").format(sum/countZh) : 0.0));
        }
    }
}


发表于 2021-12-27 16:44:32 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n = sc.nextInt();
            //负数个数
            int negCount = 0;
            //正数之和
            int pos = 0;
            //正数个数
            int posCount = 0;
            for(int i = 0; i < n; i++){
                int num = sc.nextInt();
                if(num < 0){
                    negCount++;
                }
                if(num > 0){
                    pos += num;
                    posCount++;
                }
            }
            System.out.printf("%d %.1f\n",negCount,(double)pos/posCount);
        }
    }
}

发表于 2021-12-01 15:55:36 回复(0)
import java.util.Scanner;
	public class Main {
	    public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        while (sc.hasNext()) {
                int n = sc.nextInt();
                int [] a = new int[n];
                int c1 = 0;
                int c2 = 0;
                double sum = 0;
                for(int i = 0; i < n; i++){
                    a[i] = sc.nextInt();
                    if(a[i] < 0){
                        c1 ++;
                    }
                    if(a[i] > 0){
                        c2 ++;
                        sum += a[i];
                    }
                }
                double average = sum / c2;
                System.out.printf("%d %.1f\n", c1, average);
            }
        }
    }

发表于 2021-09-16 16:40:36 回复(0)
import java.util.*;

public class Main{
    public static void main(String []args){
        Scanner scan=new Scanner(System.in);
        while(scan.hasNext()){
            int n=scan.nextInt();
            int zheng=0,fu=0;
            float sum=0;
            while(n>0){
                int x=scan.nextInt();
                if(x<0){
                    fu++;
                }else if(x>0){
                    sum+=x;
                    zheng++;
                }
                n--;
            }
            System.out.printf("%d %.1f\n",fu,sum/zheng);
		}        
	}
}

发表于 2021-09-02 14:01:43 回复(0)
为什么有一组数据输入的n后面带了个空格??
发表于 2021-08-05 12:34:13 回复(0)