题解 | #记负均正II#--统计,保留一位小数

记负均正II

https://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        int count = 0;
        int posCount = 0;
        int sum = 0;
        while (in.hasNextInt()) {
            int num = in.nextInt();
            if (num < 0) {
                count++;
            } else {
                posCount++;
                sum += num;
            }
        }
        System.out.println(count);
        // 保留一位小数
        if (posCount == 0) {
            System.out.println(0.0);
        } else {
            System.out.printf("%.01f", (sum + 0.0) / posCount);
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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