{ }codelper
平均值浮点除法输出小数P0014

平均分保留两位小数

第一行给出分数个数 n,第二行给出 n 个整数分数。
把平均分在小数第三位四舍五入(第三位是 5 或以上就进位),输出到小数第二位。
平均分是整数时也要写两位小数(85 → 85.00)。
例如平均分 0.125 输出 0.13,0.145 输出 0.15,1.005 输出 1.01。

输入
1 ≤ n ≤ 1,000,0 ≤ 分数 ≤ 100
输出
一行平均分,小数点后恰好两位
样例 1 · stdin
3
70 85 90
stdout
81.67
样例 2 · stdin
8
1 0 0 0 0 0 0 0
stdout
0.13
main.py
输入(stdin)

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

$ 就绪

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

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

陷阱有两层。
① 整数除法:C、C++、Java、C# 中 total / n 是两个 int 相除,小数会先被舍掉——用 (double) total / n 或 total * 100.0 / n 把一边变成浮点数。
Python 3 的 / 和 JS/TS/Craykon 的 / 本来就会算出小数。
② 四舍五入:浮点数是二进制,0.145 实际上是 0.14499…。
直接对平均值用格式化,各语言结果不同——Python f"{x:.2f}"、C/C++ printf("%.2f")·std::setprecision(2)、C# ToString("F2") 按实际值舍入,正好一半时取偶数(0.125 → 0.12,0.145 → 0.14);JS/TS toFixed(2) 让 0.125 → 0.13,但 0.145 → 0.14。
Java String.format("%.2f") 对最短十进制表示做 HALF_UP 舍入,直接用就对(0.15);Craykon 字符.保留小数(x, 2) 也是 5 进位,直接用就对。
其他语言请对乘以 100 的平均值 total * 100 / n 取整——正好一半时这个值能精确表示,比如 12.5。
即便如此,Python round() 和 C# Math.Round() 默认是取偶数(round(12.5) == 12),所以 Python 用 math.floor(x + 0.5),C# 用 MidpointRounding.AwayFromZero,C/C++ 用 round(),JS/TS 用 Math.round()。

Pythonmain.py · 7 行
import math n = int(input())total = sum(map(int, input().split()))# 100배 한 평균을 반올림(5는 올림). round()는 0.5를 짝수 쪽으로 보내서 쓰지 않는다cents = math.floor(total * 100 / n + 0.5)print(f"{cents / 100:.2f}")
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);// 100배 한 평균을 반올림한 뒤 다시 나눈다 — toFixed를 평균에 바로 쓰면 0.145가 0.14가 된다const cents = Math.round((total * 100) / n);console.log((cents / 100).toFixed(2));
TypeScriptmain.ts · 5 行
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);const cents: number = Math.round((total * 100) / n);console.log((cents / 100).toFixed(2));
JavaMain.java · 16 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Locale; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(br.readLine().trim());        int total = 0;        for (String s : br.readLine().trim().split("\\s+")) total += Integer.parseInt(s);        // (double)로 바꾸지 않으면 total / n이 정수 나눗셈이 된다        double avg = (double) total / n;        // Java의 %.2f는 반올림(HALF_UP)이다. Locale.ROOT로 소수점을 늘 '.'로        System.out.println(String.format(Locale.ROOT, "%.2f", avg));    }}
C#Main.cs · 15 行
using System;using System.Globalization;using System.Linq; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine().Trim());        int total = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Sum(int.Parse);        // 100배 한 평균을 0에서 먼 쪽으로 반올림(Math.Round의 기본은 짝수 쪽)        double cents = Math.Round(total * 100.0 / n, MidpointRounding.AwayFromZero);        Console.WriteLine((cents / 100).ToString("F2", CultureInfo.InvariantCulture));    }}
C++main.cpp · 14 行
#include <cmath>#include <iomanip>#include <iostream> int main() {    int n, x, total = 0;    std::cin >> n;    for (int i = 0; i < n; i++) {        std::cin >> x;        total += x;    }    double cents = std::round(total * 100.0 / n);    std::cout << std::fixed << std::setprecision(2) << cents / 100 << '\n';}
Cmain.c · 15 行
#include <math.h>#include <stdio.h> int main(void) {    int n, x, total = 0;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        scanf("%d", &x);        total += x;    }    /* 100.0을 곱해야 실수 나눗셈이 된다. round()는 0.5를 0에서 먼 쪽으로 올린다 */    double cents = round(total * 100.0 / n);    printf("%.2f\n", cents / 100);    return 0;}
Craykonmain.cray · 9 行
n = 控制台.读行().转数字();점수들 = 控制台.读数字();합 = 0;遍历 (x 在 점수들){    합 += x;}// / 는 늘 소수까지 나눈다. 자리맞추기는 5를 올리는 반올림이다控制台.输出(字符.保留小数(합 / n, 2));