题解 | #记负均正#

记负均正

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

import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }
            int num1 = 0;
            int num2 = 0;
            double total = 0.0;
            double res = 0.0;
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] < 0) {
                    num1++;
                } else if (arr[i] > 0) {
                    num2++;
                    total = total + arr[i];
                }
            }
            if (num1 == 0 && num2 == 0) {
                System.out.print(num1 + " " + 0.0);
            } else {
                res = total / num2;
                DecimalFormat df = new DecimalFormat("0.0");
                res = Double.valueOf(df.format(res));
                System.out.print(num1 + " " + res);
            }
        }
    }
}

全部评论

相关推荐

04-08 10:36
已编辑
华南理工大学 C++
点赞 评论 收藏
分享
03-28 19:11
铜陵学院 C++
有礼貌的山羊追赶太阳:太典了,连笔试都没有开始就因为HC满了而结束了,而且还卡你不让你再投其他部门的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务