题解 | 记负均正

记负均正

https://www.nowcoder.com/practice/6abde6ffcc354ea1a8333836bd6876b8

两个计数器来记录正数和负数的个数,最后保留11位小数。

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        double sum = 0.0;
        int cnt1 = 0;
        int cnt2 = 0;
        for (int i : a) {
            if (i < 0)cnt1++;
            if (i > 0) {
                sum += i;
                cnt2++;
            }
        }
        double avg = 0.0;
        if (cnt2 == 0) {
            avg = 0;
        } else {
            avg = sum / cnt2;
        }
        System.out.printf("%d %.11f", cnt1, avg);
    }
}

#牛客春招刷题训练营#
全部评论

相关推荐

秋招不是要开始了吗,我都打算润了,看大家还在找不敢润了
一条从:因为不是人人都像佬一样有实习像我们这种二本仔秋招没有实习也是白忙活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务