题解 | #记负均正#

记负均正

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);
    }
}

全部评论

相关推荐

人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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