题解 | #记负均正II#

记负均正II

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    // 注意 hasNext 和 hasNextLine 的区别
    ArrayList<Integer> list = new ArrayList<>();
    while (in.hasNextInt()) { // 注意 while 处理多个 case
      int a = in.nextInt();
      list.add(a);
    }
    long negnum = list.stream().filter(x->x < 0).count();
    double avg = list.stream().filter(x->x >= 0).mapToInt(
                   x->x).summaryStatistics().getAverage();

    System.out.println(negnum);
    System.out.printf("%.1f", avg);
  }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务