题解 | #记负均正#

记负均正

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

import java.math.BigDecimal;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    private static int n = 0;
    private static int[] an = null;
    private static int posCount = 0;
    private static int actCount = 0;
    private static int actSum = 0;

    public static void main(String[] args) {


        int i = 0;
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            if(n == 0){
                n = a;
                an = new int[n];
            } else {
                an[i] = a;
                if(a < 0){
                    posCount += 1;
                } else if (a > 0){
                    actCount += 1;
                    actSum += a;
                }
            }
        }
        result();
    }

    private static void result(){
        if(actCount == 0){
            System.out.print(posCount + " 0.0");
            return;
        }

        double d = actSum / 1.0 / actCount;
        BigDecimal bigDecimal = new BigDecimal(d);
        System.out.print(posCount + " "+ bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
    }


}

an多余了,可以去掉

#努力刷题下#
雪域灰灰刷题笔记 文章被收录于专栏

雪域灰灰刷题笔记

全部评论

相关推荐

点赞 评论 收藏
分享
09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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