n = int(input())total = 0.0out = []for _ in range(n): total += float(input()) # 소수 셋째 자리까지 찍고 끝의 0과 남은 소수점을 지운다 s = f"{total:.3f}".rstrip("0").rstrip(".") if s == "-0": # 0 바로 아래의 작은 오차는 "-0.000"으로 찍힌다 s = "0" out.append(s)print("\n".join(out))
JavaScriptmain.js · 11 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);let total = 0;const out = [];for (let i = 1; i <= n; i++) { total += Number(lines[i]); // 1000분의 1로 반올림한 수를 그대로 찍으면 끝의 0이 없다(2.5, 2). -0도 "0"으로 찍힌다 const shown = Math.round(total * 1000) / 1000; out.push(String(shown));}console.log(out.join("\n"));
TypeScriptmain.ts · 12 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);let total = 0;const out: string[] = [];for (let i = 1; i <= n; i++) { total += Number(lines[i]); // 1000분의 1로 반올림한 수를 그대로 찍으면 끝의 0이 없다(2.5, 2). -0도 "0"으로 찍힌다 const shown: number = Math.round(total * 1000) / 1000; out.push(String(shown));}console.log(out.join("\n"));
JavaMain.java · 22 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Locale;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); double total = 0; StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { total += Double.parseDouble(in.readLine().trim()); // Double.toString은 2.0처럼 찍으니 소수 셋째 자리로 찍고 끝의 0을 지운다 String s = String.format(Locale.ROOT, "%.3f", total); s = s.replaceAll("0+$", "").replaceAll("\\.$", ""); if (s.equals("-0")) s = "0"; out.append(s).append('\n'); } System.out.print(out); }}
C#Main.cs · 22 行
using System;using System.Globalization;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); double total = 0; var sb = new StringBuilder(); for (int i = 0; i < n; i++) { total += double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture); // "F3"으로 셋째 자리까지 찍고 끝의 0과 소수점을 지운다 string s = total.ToString("F3", CultureInfo.InvariantCulture).TrimEnd('0').TrimEnd('.'); if (s == "-0") s = "0"; sb.Append(s).Append('\n'); } Console.Write(sb); }}
C++main.cpp · 24 行
#include <iomanip>#include <iostream>#include <sstream>#include <string>int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; double total = 0; for (int i = 0; i < n; i++) { double w; std::cin >> w; total += w; std::ostringstream os; os << std::fixed << std::setprecision(3) << total; std::string s = os.str(); // 끝의 0과 남은 소수점을 지운다 while (s.back() == '0') s.pop_back(); if (s.back() == '.') s.pop_back(); if (s == "-0") s = "0"; std::cout << s << "\n"; }}
Cmain.c · 22 行
#include <stdio.h>#include <string.h>int main(void) { int n; double w, total = 0; char buf[64]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf", &w); total += w; snprintf(buf, sizeof buf, "%.3f", total); /* 끝의 0을 지우고, 소수점만 남으면 그것도 지운다 */ size_t len = strlen(buf); while (buf[len - 1] == '0') len--; if (buf[len - 1] == '.') len--; buf[len] = '\0'; if (strcmp(buf, "-0") == 0) strcpy(buf, "0"); puts(buf); } return 0;}
Craykonmain.cray · 9 行
n = 控制台.读行().转数字();total = 0;循环 (i = 0; i < n; i++){ total += 控制台.读行().去空白().转数字(); // 1000분의 1 단위로 맞춘다. 쓰기는 끝의 0 없이 찍는다(2.5, 2) shown = 数学.向下取整(total * 1000 + 0.5) / 1000; 控制台.输出(shown);}