첫 줄에 점수의 개수 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
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
함정이 두 겹이에요. ① 정수 나눗셈: C·C++·Java·C#에서 total / n은 int끼리라 소수가 먼저 버려져요 — (double) total / n이나 total * 100.0 / n처럼 한쪽을 실수로. Python 3의 /, JS·TS·크레이콘의 /는 원래 소수까지 나눠요. ② 반올림: 실수는 이진수라 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), 크레이콘 글자.소수자리맞추기(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 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 줄
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));