平均以上だった朝
n 日間、毎朝の気温を測った。
一行目に n が与えられる。
二行目に朝の気温 n 個が測った順に空白一つずつで区切って与えられる。
平均は気温をすべて足して n で割った値であり、小数点以下を切り捨てたり四捨五入したりせずにそのまま使う。
朝の気温が平均以上である日の数を出力せよ。
気温が平均とちょうど同じ日も数える。
- 入力
- 1 ≤ n ≤ 1,000、-50 ≤ 気温 ≤ 50(整数)
- 出力
- 気温が平均以上である日の数を一行に
4 1 2 2 5
1
3 7 7 7
3
n 日間、毎朝の気温を測った。
一行目に n が与えられる。
二行目に朝の気温 n 個が測った順に空白一つずつで区切って与えられる。
平均は気温をすべて足して n で割った値であり、小数点以下を切り捨てたり四捨五入したりせずにそのまま使う。
朝の気温が平均以上である日の数を出力せよ。
気温が平均とちょうど同じ日も数える。
4 1 2 2 5
1
3 7 7 7
3
実行すると、これらの行が順に input()・標準入力に渡されます(実行中に入力を求めません)。
$ 準備完了
コードを入力して解くにはパソコンで開いてください。モバイルでは問題と解答を見られます。
条件に合うものを数える書き方は言語ごとに違います。
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 になります。
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))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);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);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); }}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)); }}#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";}#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;}n = コンソール.行読み().数に();a = コンソール.数読み();합 = 0;それぞれ (x の中 a){ 합 += x;}// 크레이콘의 /는 실수 나눗셈이라 평균이 2.5처럼 그대로 나온다평균 = 합 / n;개수 = 0;それぞれ (x の中 a){ // 참/거짓은 수에 더할 수 없으므로 만약으로 센다 もし (x >= 평균) { 개수 += 1; }}コンソール.書く(개수);