首页 > 试题广场 >

记负均正

[编程题]记负均正
  • 热度指数:261778 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
首先输入要输入的整数个数n,然后输入n个整数。输出为n个整数中负数的个数,和所有正整数的平均值,结果保留一位小数。
0即不是正整数,也不是负数,不计入计算。如果没有正数,则平均值为0。

数据范围: ,输入的整数都满足

输入描述:

首先输入一个正整数n,
然后输入n个整数。



输出描述:

输出负数的个数,和所有正整数的平均值。

示例1

输入

11 
1 2 3 4 5 6 7 8 9 0 -1

输出

1 5.0
示例2

输入

3
0 0 0

输出

0 0.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)
//一种简单的做法
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        while(s.hasNext()){
            int n = s.nextInt();
            int[] arr = new int[n];
            
            int countf = 0;
            int countz = 0;
            double sum = 0.0;
            double p = 0.0;
            for(int i = 0;i < n;i++){
                arr[i] = s.nextInt();
                if(arr[i] < 0){
                    countf++;
                }
                if(arr[i] > 0){
                    countz++;
                    sum += arr[i];
                    p = (double)(sum / countz);
                }
            }
            System.out.println(countf + " " + String.format("%.1f",p));
        }
    }
}
发表于 2021-08-03 12:08:19 回复(0)
这通过率和难度是开玩笑的吧?????????
发表于 2021-07-24 19:04:25 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int num = sc.nextInt();
        int[] arr = new int[num];
        boolean flag = true;
        int count = 0;
        while(sc.hasNext()&&flag){
            for(int i = 0 ; i < arr.length; i++){
                arr[i] = sc.nextInt();
                count++;
            }
            if(count==num){
                flag = false;
            }
        }
        float count1 = 0;
        int count2 = 0;
        float sum = 0;
        for(int i = 0; i < arr.length; i++){
            if(arr[i]>0){
                count1++;
                sum+=arr[i];
            }
            if(arr[i]<0){
                count2++;
            }
        }
             System.out.print(count2+" ");
             System.out.printf("%.1f\n",sum/count1);
        }
       
    }
}
发表于 2021-06-12 11:35:43 回复(0)

问题信息

难度:
36条回答 23533浏览

热门推荐

通过挑战的用户

查看代码