题解 | #记负均正#

记负均正

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            // 正整数之和
            int sum = 0;
            // 统计负数个数
            int negativeCount = 0;
            // 统计正整数的个数
            int positiveCount = 0;
            for (int i = 0; i < n; i++) {
                int digit = sc.nextInt();
                if (digit < 0) {
                    negativeCount++;
                } else if (digit > 0) {
                    sum += digit;
                    positiveCount++;
                }
            }
            String format = "0.0";
            if (positiveCount > 0) {
                format = String.format("%.1f", sum * 1.0 / positiveCount);
            }
            System.out.print(negativeCount + " " + format);
        }
        sc.close();
    }
}
全部评论

相关推荐

04-03 15:12
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
肥肠椒绿:双非本可不就犯天条了,双非本就应该打入无间地狱
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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