A school year has m classes.
The first line gives m.
Each of the next m lines gives, for one class, its number of students k followed by the k students' scores, separated by spaces.
For each class, print its average score on its own line, in input order.
On the last line, print the year average.
The year average is the sum of the scores of every student in the year divided by the total number of students in the year.
Round every average at the third decimal place and write it with exactly two digits after the decimal point (85 → 85.00).
To keep rounding unambiguous, every average to print is such that, multiplied by 100, its fractional part is 0.4 or less or 0.6 or more.
Input
1 ≤ m ≤ 100, 1 ≤ k ≤ 100, 0 ≤ score ≤ 100 (integers)
Output
m lines of class averages, then the year average (all with two decimals)
Example 1 · stdin
2
3 90 80 70
1 100
stdout
80.00
100.00
85.00
Example 2 · stdin
2
3 0 0 1
2 100 100
stdout
0.33
100.00
40.20
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
The biggest trap is the average of averages. Adding up the class averages and dividing by the number of classes differs from the year average when classes have different sizes. Averaging a 3-student class with average 80 and a 1-student class with average 100 gives 90, but the average of the 4 students is 340 ÷ 4 = 85. So collect each class's score sum and student count, and divide only at the end. Start each class sum again from 0. The second trap is integer division. In C, C++, Java and C#, s / k divides two ints and keeps only the quotient. Convert first and then divide, as in (double)s / k (C++ static_cast<double>(s) / k). double avg = s / k; or (double)(s / k) comes after the fraction is already gone. In Python 3, JS, TS and Craykon, / always keeps decimals. Two decimals are Python f"{x:.2f}", JS/TS x.toFixed(2), C printf("%.2f"), Java String.format(Locale.ROOT, "%.2f", x), C# x.ToString("F2", CultureInfo.InvariantCulture) and Craykon Str.Fixed(x, 2). In C++ always add std::fixed. std::setprecision(2) alone means two significant digits, not two decimals, so 33.33 prints as 33. The student count and the scores come on one line, so it is handy to split them with Python k, *scores = map(int, line.split()) or JS/TS const [k, ...scores] = ….
Pythonmain.py · 11 lines
m = int(input())out = []total = count = 0for _ in range(m): k, *scores = map(int, input().split()) s = sum(scores) out.append(f"{s / k:.2f}") total += s # 학년 평균은 반 평균이 아니라 점수 합과 학생 수를 모아서 구한다 count += kout.append(f"{total / count:.2f}")print("\n".join(out))
JavaScriptmain.js · 15 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const m = Number(lines[0]);const out = [];let total = 0;let count = 0;for (let i = 1; i <= m; i++) { const [k, ...scores] = lines[i].trim().split(/\s+/).map(Number); let s = 0; for (const x of scores) s += x; out.push((s / k).toFixed(2)); total += s; // 학년 평균은 점수 합과 학생 수를 모아서 구한다 count += k;}out.push((total / count).toFixed(2));console.log(out.join("\n"));
TypeScriptmain.ts · 16 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const m: number = Number(lines[0]);const out: string[] = [];let total = 0;let count = 0;for (let i = 1; i <= m; i++) { const [k, ...scores]: number[] = lines[i].trim().split(/\s+/).map(Number); let s = 0; for (const x of scores) s += x; out.push((s / k).toFixed(2)); total += s; // 학년 평균은 점수 합과 학생 수를 모아서 구한다 count += k;}out.push((total / count).toFixed(2));console.log(out.join("\n"));
JavaMain.java · 26 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Locale;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int m = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); int total = 0, count = 0; for (int i = 0; i < m; i++) { StringTokenizer st = new StringTokenizer(in.readLine()); int k = Integer.parseInt(st.nextToken()); int s = 0; for (int j = 0; j < k; j++) s += Integer.parseInt(st.nextToken()); // int끼리 나누면 몫만 남으니 한쪽을 double로 out.append(String.format(Locale.ROOT, "%.2f", (double) s / k)).append('\n'); total += s; count += k; } out.append(String.format(Locale.ROOT, "%.2f", (double) total / count)).append('\n'); System.out.print(out); }}
C#Main.cs · 26 lines
using System;using System.Globalization;using System.Linq;using System.Text;class Program{ static void Main() { int m = int.Parse(Console.ReadLine()); var sb = new StringBuilder(); int total = 0, count = 0; for (int i = 0; i < m; i++) { int[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); int k = p[0]; int s = p.Skip(1).Sum(); // int끼리 나누면 몫만 남으니 한쪽을 double로 sb.Append(((double)s / k).ToString("F2", CultureInfo.InvariantCulture)).Append('\n'); total += s; count += k; } sb.Append(((double)total / count).ToString("F2", CultureInfo.InvariantCulture)).Append('\n'); Console.Write(sb); }}
C++main.cpp · 24 lines
#include <iomanip>#include <iostream>int main() { std::ios::sync_with_stdio(false); int m; std::cin >> m; int total = 0, count = 0; std::cout << std::fixed << std::setprecision(2); for (int i = 0; i < m; i++) { int k; std::cin >> k; int s = 0; for (int j = 0; j < k; j++) { int x; std::cin >> x; s += x; } std::cout << static_cast<double>(s) / k << "\n"; total += s; count += k; } std::cout << static_cast<double>(total) / count << "\n";}
Cmain.c · 20 lines
#include <stdio.h>int main(void) { int m, k, x; int total = 0, count = 0; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &k); int s = 0; for (int j = 0; j < k; j++) { scanf("%d", &x); s += x; } printf("%.2f\n", (double)s / k); /* (double)로 바꾼 뒤에 나눈다 */ total += s; count += k; } printf("%.2f\n", (double)total / count); return 0;}
Craykonmain.cray · 17 lines
m = Console.ReadLine().ToNumber();total = 0;count = 0;for (i = 0; i < m; i++){ p = Console.ReadNumbers(); k = p[0]; s = 0; for (j = 1; j <= k; j++) { s += p[j]; } Console.WriteLine(Str.Fixed(s / k, 2)); total += s; // 학년 평균은 점수 합과 학생 수를 모아서 구한다 count += k;}Console.WriteLine(Str.Fixed(total / count, 2));