The first line gives the number of scores n, and the second line gives n integer scores. Print their average rounded to two decimal places, rounding half up (if the third decimal is 5 or more, round up). Always print two decimals even when the average is a whole number (85 → 85.00). For example, an average of 0.125 prints 0.13, 0.145 prints 0.15, and 1.005 prints 1.01.
Input
1 ≤ n ≤ 1,000, 0 ≤ score ≤ 100
Output
the average on one line with exactly two decimals
Example 1 · stdin
3
70 85 90
stdout
81.67
Example 2 · stdin
8
1 0 0 0 0 0 0 0
stdout
0.13
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
There are two traps. (1) Integer division: in C, C++, Java and C#, total / n divides two ints and drops the fraction first — make one side a double with (double) total / n or total * 100.0 / n. Python 3 / and JS/TS/Craykon / already divide with decimals. (2) Rounding: doubles are binary, so 0.145 is really 0.14499…. Formatting the average directly gives different answers per language — Python f"{x:.2f}", C/C++ printf("%.2f") / std::setprecision(2) and C# ToString("F2") round the exact binary value with ties to even (0.125 → 0.12, 0.145 → 0.14); JS/TS toFixed(2) gives 0.125 → 0.13 but 0.145 → 0.14. Java String.format("%.2f") rounds the shortest decimal form HALF_UP, so it is right as is (0.15), and Craykon Str.Fixed(x, 2) also rounds 5 up and is right as is. Elsewhere, round the average times 100, total * 100 / n — on an exact tie this is exactly representable, like 12.5. Even then Python round() and C# Math.Round() default to ties-to-even (round(12.5) == 12), so use math.floor(x + 0.5) in Python, MidpointRounding.AwayFromZero in C#, round() in C/C++ and Math.round() in JS/TS.
Pythonmain.py · 7 lines
import mathn = 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 lines
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 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);const cents: number = Math.round((total * 100) / n);console.log((cents / 100).toFixed(2));
JavaMain.java · 16 lines
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 lines
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 lines
#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 lines
#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 lines
n = Console.ReadLine().ToNumber();점수들 = Console.ReadNumbers();합 = 0;foreach (x in 점수들){ 합 += x;}// / 는 늘 소수까지 나눈다. 자리맞추기는 5를 올리는 반올림이다Console.WriteLine(Str.Fixed(합 / n, 2));