The morning temperature was measured every day for n days.
The first line gives n.
The second line gives the n morning temperatures in the order measured, separated by single spaces.
The average is the sum of all the temperatures divided by n, used as it is, without dropping or rounding the fractional part.
Print the number of days whose morning temperature is greater than or equal to the average.
A day whose temperature is exactly equal to the average is counted too.
Input
1 ≤ n ≤ 1,000, -50 ≤ temperature ≤ 50 (integers)
Output
One line with the number of days whose temperature is greater than or equal to the average
Example 1 · stdin
4
1 2 2 5
stdout
1
Example 2 · stdin
3
7 7 7
stdout
3
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Each language has its own way to count the items that satisfy a condition. Python adds them up with true counted as 1, as in sum(x * n >= total for x in a). JS and TS use a.filter(x => x >= avg).length, C# a.Count(x => x * n >= total), Java Arrays.stream(a).filter(x -> x * n >= total).count(), and C++ std::count_if. In C the result of a comparison is 1 or 0, so you can add it with count += (a[i] * n >= total). In Java and C# count += (x >= avg) does not compile, and Craykon also stops with an error when you add true/false to a number, so count with if. Trap one: the division for the average. In C, C++, Java and C#, dividing integers with total / n drops the fractional part. The average in example 1 is 2.5, but it becomes 2, so the two days at 2 degrees are counted as well and the answer becomes 3. Python // also gives 2 and the same 3. The / of Python, JS, TS and Craykon is real division, so 2.5 comes out as it is. To use integers only, multiply both sides of x ≥ total ÷ n by n and compare x * n >= total. When the average is negative the two divisions differ. For the temperatures -4 -3 -2 -1 the average is -2.5 and the answer is 2. C / drops toward 0 and gives -2, so it prints 2 by luck, but Python // goes down to -3 and prints 3. Trap two: 'greater than or equal' is >=. With > the days exactly equal to the average are left out, and example 2 becomes 0 instead of 3.
Pythonmain.py · 6 lines
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 lines
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 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
n = Console.ReadLine().ToNumber();a = Console.ReadNumbers();합 = 0;foreach (x in a){ 합 += x;}// 크레이콘의 /는 실수 나눗셈이라 평균이 2.5처럼 그대로 나온다평균 = 합 / n;개수 = 0;foreach (x in a){ // 참/거짓은 수에 더할 수 없으므로 만약으로 센다 if (x >= 평균) { 개수 += 1; }}Console.WriteLine(개수);