import sysdef round_half_away(s, k): # s / k 를 가장 가까운 정수로, 딱 반이면 0에서 먼 쪽으로(정수만 써서 정확하게) q = (2 * abs(s) + k) // (2 * k) return q if s >= 0 else -qdef fmt(t): # t는 0.1도 단위 정수: -5 → "-0.5", 0 → "0.0" sign = "-" if t < 0 else "" a = abs(t) return f"{sign}{a // 10}.{a % 10}"def main(): lines = sys.stdin.read().split("\n") d = int(lines[0]) out = [] total = 0 count = 0 for i in range(1, d + 1): parts = lines[i].split() k = int(parts[0]) # "-3.5" → "-35": 소수점을 지워 0.1도 단위 정수로 읽는다 s = sum(int(p.replace(".", "")) for p in parts[1:k + 1]) out.append(fmt(round_half_away(s, k))) total += s count += k out.append(fmt(round_half_away(total, count))) print("\n".join(out))main()
JavaScriptmain.js · 33 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const d = Number(lines[0]);// s / k 를 가장 가까운 정수로, 딱 반이면 0에서 먼 쪽으로// Math.round는 딱 반을 +∞ 쪽으로 보내서(-2.5 → -2) 절댓값에 쓰고 부호를 다시 붙인다function roundHalfAway(s, k) { const q = Math.round(Math.abs(s) / k); return s < 0 ? -q : q;}// t는 0.1도 단위 정수: -5 → "-0.5", 0 → "0.0"function fmt(t) { const sign = t < 0 ? "-" : ""; const a = Math.abs(t); return `${sign}${Math.floor(a / 10)}.${a % 10}`;}const out = [];let total = 0;let count = 0;for (let i = 1; i <= d; i++) { const parts = lines[i].trim().split(" "); const k = Number(parts[0]); let s = 0; for (let j = 1; j <= k; j++) { s += Number(parts[j].replace(".", "")); // "-3.5" → -35 } out.push(fmt(roundHalfAway(s, k))); total += s; count += k;}out.push(fmt(roundHalfAway(total, count)));console.log(out.join("\n"));
TypeScriptmain.ts · 34 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const d: number = Number(lines[0]);// s / k 를 가장 가까운 정수로, 딱 반이면 0에서 먼 쪽으로// Math.round는 딱 반을 +∞ 쪽으로 보내서(-2.5 → -2) 절댓값에 쓰고 부호를 다시 붙인다function roundHalfAway(s: number, k: number): number { const q = Math.round(Math.abs(s) / k); return s < 0 ? -q : q;}// t는 0.1도 단위 정수: -5 → "-0.5", 0 → "0.0"function fmt(t: number): string { const sign = t < 0 ? "-" : ""; const a = Math.abs(t); return `${sign}${Math.floor(a / 10)}.${a % 10}`;}const out: string[] = [];let total = 0;let count = 0;for (let i = 1; i <= d; i++) { const parts: string[] = lines[i].trim().split(" "); const k = Number(parts[0]); let s = 0; for (let j = 1; j <= k; j++) { s += Number(parts[j].replace(".", "")); // "-3.5" → -35 } out.push(fmt(roundHalfAway(s, k))); total += s; count += k;}out.push(fmt(roundHalfAway(total, count)));console.log(out.join("\n"));
JavaMain.java · 40 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { // s / k 를 가장 가까운 정수로, 딱 반이면 0에서 먼 쪽으로(정수만 써서 정확하게) // Math.round는 딱 반을 +∞ 쪽으로 보내서(-2.5 → -2) 쓰지 않는다 static long roundHalfAway(long s, long k) { long q = (2 * Math.abs(s) + k) / (2 * k); return s < 0 ? -q : q; } // t는 0.1도 단위 정수: -5 → "-0.5", 0 → "0.0" static String fmt(long t) { String sign = t < 0 ? "-" : ""; long a = Math.abs(t); return sign + (a / 10) + "." + (a % 10); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int d = Integer.parseInt(br.readLine().trim()); StringBuilder sb = new StringBuilder(); long total = 0; long count = 0; for (int i = 0; i < d; i++) { String[] parts = br.readLine().trim().split(" "); int k = Integer.parseInt(parts[0]); long s = 0; for (int j = 1; j <= k; j++) { s += Integer.parseInt(parts[j].replace(".", "")); // "-3.5" → -35 } sb.append(fmt(roundHalfAway(s, k))).append('\n'); total += s; count += k; } sb.append(fmt(roundHalfAway(total, count))).append('\n'); System.out.print(sb); }}
C#Main.cs · 43 行
using System;using System.Text;class Program{ // s / k 를 가장 가까운 정수로, 딱 반이면 0에서 먼 쪽으로 // Math.Round의 기본은 짝수 쪽이라 AwayFromZero를 준다 static long RoundHalfAway(long s, long k) { return (long)Math.Round((double)s / k, MidpointRounding.AwayFromZero); } // t는 0.1도 단위 정수: -5 → "-0.5", 0 → "0.0" static string Fmt(long t) { string sign = t < 0 ? "-" : ""; long a = Math.Abs(t); return sign + (a / 10) + "." + (a % 10); } static void Main() { int d = int.Parse(Console.ReadLine().Trim()); var sb = new StringBuilder(); long total = 0; long count = 0; for (int i = 0; i < d; i++) { string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int k = int.Parse(parts[0]); long s = 0; for (int j = 1; j <= k; j++) { s += int.Parse(parts[j].Replace(".", "")); // "-3.5" → -35 } sb.Append(Fmt(RoundHalfAway(s, k))).Append('\n'); total += s; count += k; } sb.Append(Fmt(RoundHalfAway(total, count))).Append('\n'); Console.Write(sb); }}
C++main.cpp · 40 行
#include <cmath>#include <cstdio>#include <cstdlib>#include <iostream>#include <string>// s / k 를 가장 가까운 정수로, 딱 반이면 0에서 먼 쪽으로: std::lround가 바로 그 규칙이다long roundHalfAway(long s, long k) { return std::lround(static_cast<double>(s) / k);}// t는 0.1도 단위 정수: -5 → "-0.5", 0 → "0.0"std::string fmt(long t) { long a = std::labs(t); return std::string(t < 0 ? "-" : "") + std::to_string(a / 10) + "." + std::to_string(a % 10);}int main() { // 수가 10만 개까지라 scanf로 읽는다(macOS의 libc++ cin은 실수를 읽는 데 이 크기에서 0.5초쯤 걸린다) int d; if (std::scanf("%d", &d) != 1) return 0; std::string out; long total = 0, count = 0; for (int i = 0; i < d; i++) { int k; std::scanf("%d", &k); long s = 0; for (int j = 0; j < k; j++) { double x; std::scanf("%lf", &x); s += std::lround(x * 10); // -3.5 → -35: 10을 곱한 값은 정수에 아주 가까우니 반올림해 정수로 } out += fmt(roundHalfAway(s, k)) + "\n"; total += s; count += k; } out += fmt(roundHalfAway(total, count)) + "\n"; std::cout << out; return 0;}
Cmain.c · 35 行
#include <math.h>#include <stdio.h>#include <stdlib.h>/* s / k 를 가장 가까운 정수로, 딱 반이면 0에서 먼 쪽으로: lround가 바로 그 규칙이다 */long round_half_away(long s, long k) { return lround((double)s / k);}/* t는 0.1도 단위 정수: -5 → "-0.5", 0 → "0.0" */void print_tenths(long t) { long a = labs(t); printf("%s%ld.%ld\n", t < 0 ? "-" : "", a / 10, a % 10);}int main(void) { int d; if (scanf("%d", &d) != 1) return 0; long total = 0, count = 0; for (int i = 0; i < d; i++) { int k; scanf("%d", &k); long s = 0; for (int j = 0; j < k; j++) { double x; scanf("%lf", &x); s += lround(x * 10); /* -3.5 → -35: 10을 곱한 값은 정수에 아주 가까우니 반올림해 정수로 */ } print_tenths(round_half_away(s, k)); total += s; count += k; } print_tenths(round_half_away(total, count)); return 0;}
Craykonmain.cray · 30 行
// t는 0.1도 단위 정수: -5 → "-0.5", 0 → "0.0"표시(t){ 부호 = ""; 如果 (t < 0) { 부호 = "-"; } a = 数学.绝对值(t); 返回 부호 + 数学.商(a, 10) + "." + a % 10;}d = 控制台.读行().转数字();total = 0;count = 0;循环 (i = 0; i < d; i++){ parts = 控制台.读行().去空白().分割(" "); k = parts[0].转数字(); s = 0; 循环 (j = 1; j <= k; j++) { s = s + parts[j].替换(".", "").转数字(); // "-3.5" → -35 } // 수학.반올림은 딱 반을 0에서 먼 쪽으로 보낸다(-2.5 → -3) 控制台.输出(표시(数学.四舍五入(s / k))); total = total + s; count = count + k;}控制台.输出(표시(数学.四舍五入(total / count)));