题解 | #记负均正#

记负均正

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

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int countL0 = 0;
        int countG0 = 0;
        int sum = 0;

        for(int i=0; i<n; i++){
            int num = sc.nextInt();
            if(num < 0) countL0++;
            if(num > 0){
                sum += num;
                countG0++;
            }
        }

        String avg = "0.0"; // .format()返回string
        if(countG0 != 0){
            DecimalFormat df = new DecimalFormat("0.0"); 
            avg = df.format((float)sum/(float)countG0); //java除法保留小数:int强转为float
        }
        System.out.println(countL0 + " " + avg);
    }
}

全部评论

相关推荐

叁六玖:不买课还想秋招
点赞 评论 收藏
分享
牛客吹哨人:哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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