{ }codelper
计数平均值P0343

不低于平均的早晨

连续 n 天,每天早晨都测了气温。
第一行给出 n。
第二行按测量顺序给出 n 个早晨气温,相邻两个之间用一个空格隔开。
平均值是所有气温之和除以 n 得到的值,不舍去小数部分也不四舍五入,原样使用。
输出早晨气温大于或等于平均值的天数。
气温与平均值正好相等的那天也要算进去。

输入
1 ≤ n ≤ 1,000,-50 ≤ 气温 ≤ 50(整数)
输出
一行,气温大于或等于平均值的天数
样例 1 · stdin
4
1 2 2 5
stdout
1
样例 2 · stdin
3
7 7 7
stdout
3
main.py
输入(stdin)

运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。

$ 就绪

请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。

其他语言怎么写?查看各语言题解 · 8 种语言题解

统计满足条件的个数,各语言写法不一样。
Python 像 sum(x * n >= total for x in a) 这样把真当作 1 加起来。
JS、TS 用 a.filter(x => x >= avg).length,C# 用 a.Count(x => x * n >= total),Java 用 Arrays.stream(a).filter(x -> x * n >= total).count(),C++ 用 std::count_if。
C 中比较的结果是 1 或 0,所以可以写 count += (a[i] * n >= total) 直接相加。
Java 和 C# 中 count += (x >= avg) 无法通过编译,Craykon 把真假加到数上也会报错停下,所以要用 如果 来数。
陷阱一:求平均值的除法。
在 C、C++、Java、C# 中,整数之间用 total / n 相除会舍去小数部分。
例 1 的平均值是 2.5,却变成 2,于是连 2 度的那两天也算进去,输出 3。
Python 的 // 同样得到 2,也输出 3。
Python、JS、TS、Craykon 的 / 是实数除法,2.5 会原样得到。
如果只想用整数,就把 x ≥ total ÷ n 的两边都乘以 n,用 x * n >= total 来比较。
平均值为负数时,两种除法的结果不同。
气温为 -4 -3 -2 -1 时,平均值是 -2.5,答案是 2。
C 的 / 向 0 舍去得到 -2,所以碰巧输出 2;而 Python 的 // 向下取整得到 -3,所以输出 3。
陷阱二:“大于或等于”是 >=。
写成 > 就会漏掉与平均值正好相等的那几天,例 2 输出的不是 3 而是 0。

Pythonmain.py · 6 行
n = int(input())a = list(map(int, input().split()))total = sum(a)# x >= total / n 의 양쪽에 n을 곱하면 나눗셈 없이 정수끼리 견줄 수 있다# 참은 1, 거짓은 0으로 더해지므로 sum이 곧 개수다print(sum(x * n >= total for x in a))
JavaScriptmain.js · 5 行
const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const total = a.reduce((s, x) => s + x, 0);// JS의 /는 늘 실수 나눗셈이라 평균이 2.5처럼 그대로 나온다const avg = total / n;console.log(a.filter((x) => x >= avg).length);
TypeScriptmain.ts · 6 行
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const total: number = a.reduce((s, x) => s + x, 0);// TS의 /는 늘 실수 나눗셈이라 평균이 2.5처럼 그대로 나온다const avg: number = total / n;console.log(a.filter((x) => x >= avg).length);
JavaMain.java · 15 行
import java.util.Arrays;import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        int[] a = new int[n];        for (int i = 0; i < n; i++) a[i] = sc.nextInt();        int total = Arrays.stream(a).sum();        // int끼리 나누면 소수점 아래가 버려지므로 양쪽에 n을 곱해 견준다        long count = Arrays.stream(a).filter(x -> x * n >= total).count();        System.out.println(count);    }}
C#Main.cs · 14 行
using System;using System.Linq; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        int[] a = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse);        int total = a.Sum();        // int끼리 나누면 소수점 아래가 버려지므로 양쪽에 n을 곱해 견준다        Console.WriteLine(a.Count(x => x * n >= total));    }}
C++main.cpp · 14 行
#include <algorithm>#include <iostream>#include <numeric>#include <vector> int main() {    int n;    std::cin >> n;    std::vector<int> a(n);    for (int& x : a) std::cin >> x;    int total = std::accumulate(a.begin(), a.end(), 0);    // int끼리 나누면 소수점 아래가 버려지므로 양쪽에 n을 곱해 견준다    std::cout << std::count_if(a.begin(), a.end(), [&](int x) { return x * n >= total; }) << "\n";}
Cmain.c · 18 行
#include <stdio.h> int main(void) {    int n, a[1000], total = 0;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        scanf("%d", &a[i]);        total += a[i];    }    int count = 0;    for (int i = 0; i < n; i++) {        /* total / n은 소수점 아래를 버리므로 양쪽에 n을 곱해 견준다.           C에서 비교의 결과는 int 1 또는 0이라 그대로 더할 수 있다 */        count += (a[i] * n >= total);    }    printf("%d\n", count);    return 0;}
Craykonmain.cray · 19 行
n = 控制台.读行().转数字();a = 控制台.读数字();합 = 0;遍历 (x 在 a){    합 += x;}// 크레이콘의 /는 실수 나눗셈이라 평균이 2.5처럼 그대로 나온다평균 = 합 / n;개수 = 0;遍历 (x 在 a){    // 참/거짓은 수에 더할 수 없으므로 만약으로 센다    如果 (x >= 평균)    {        개수 += 1;    }}控制台.输出(개수);