题解 | #记负均正II#

记负均正II

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int sum = 0;
        int negaCnt = 0;
        int zeroCnt = 0;
        int otherCnt = 0;
        while(in.hasNextInt()) {
            int temp = in.nextInt();
            if(temp == 0) {
                zeroCnt++;
            } else if(temp < 0) {
                negaCnt++;
            } else {
                otherCnt++;
                sum+=temp;
            }
        }
        System.out.println(negaCnt);
        if(sum == 0) {
            System.out.println("0.0");
        } else {
            String avg = String.format("%.1f", (sum/(double)otherCnt));
            System.out.print(avg);
        }
    }
}

全部评论

相关推荐

06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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