一行目に数の個数 n が与えられ、続く n 行に実数 x が一行に一つずつ与えられる。 x は整数か、小数点以下一、二桁までの数である(例:7、-3.7、0.50)。 各 x について、三つの整数を空白一つで区切って一行に出力せよ。 ① 切り捨て:小数部分を取り除いた値(0 の方へ、-3.7 → -3)。 ② 床(floor):x を超えない最大の整数(-3.7 → -4)。 ③ 四捨五入:x に最も近い整数で、ちょうど半分(.5)なら 0 から遠い方(2.5 → 3、-2.5 → -3)。 0 は符号なしで 0 と書く。
import mathdef round_half_away(x): # Python의 round()는 딱 반이면 짝수 쪽(round(2.5) == 2)이라 직접 만든다 return int(math.copysign(math.floor(abs(x) + 0.5), x))n = int(input())for _ in range(n): x = float(input()) print(int(x), math.floor(x), round_half_away(x))
JavaScriptmain.js · 11 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const out = [];for (let i = 1; i <= n; i++) { const x = Number(lines[i]); // Math.round(-2.5)는 -2(위쪽으로)라 크기로 반올림하고 부호를 붙인다 const r = Math.sign(x) * Math.round(Math.abs(x)); // -0은 문자열로 바꾸면 "0"이 된다 out.push([Math.trunc(x), Math.floor(x), r].join(" "));}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]);const out: string[] = [];for (let i = 1; i <= n; i++) { const x: number = Number(lines[i]); // Math.round(-2.5)는 -2(위쪽으로)라 크기로 반올림하고 부호를 붙인다 const r: number = Math.sign(x) * Math.round(Math.abs(x)); // -0은 문자열로 바꾸면 "0"이 된다 out.push([Math.trunc(x), Math.floor(x), r].join(" "));}console.log(out.join("\n"));
JavaMain.java · 20 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;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()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { double x = Double.parseDouble(in.readLine().trim()); int t = (int) x; // (int)는 0 쪽으로 버린다 int f = (int) Math.floor(x); // Math.round(-2.5)는 -2라 크기로 반올림하고 부호를 붙인다 int r = (int) Math.round(Math.abs(x)) * (x < 0 ? -1 : 1); out.append(t).append(' ').append(f).append(' ').append(r).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()); var out_ = new StringBuilder(); for (int i = 0; i < n; i++) { double x = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture); int t = (int)x; // (int)는 0 쪽으로 버린다 int f = (int)Math.Floor(x); // Math.Round의 기본은 짝수 쪽 반올림이라 0에서 먼 쪽을 지정한다 int r = (int)Math.Round(x, MidpointRounding.AwayFromZero); out_.Append(t).Append(' ').Append(f).Append(' ').Append(r).Append('\n'); } Console.Write(out_); }}
C++main.cpp · 14 行
#include <cmath>#include <iostream>int main() { int n; double x; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> x; // static_cast<int>는 0 쪽으로 버리고, std::round는 딱 반이면 0에서 먼 쪽이다 std::cout << static_cast<int>(x) << ' ' << static_cast<int>(std::floor(x)) << ' ' << static_cast<int>(std::round(x)) << '\n'; }}
Cmain.c · 14 行
#include <math.h>#include <stdio.h>int main(void) { int n; double x; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf", &x); /* (int)는 0 쪽으로 버리고, round()는 딱 반이면 0에서 먼 쪽이다 */ printf("%d %d %d\n", (int)x, (int)floor(x), (int)round(x)); } return 0;}
Craykonmain.cray · 9 行
n = コンソール.行読み().数に();繰り返す (i = 0; i < n; i++){ x = コンソール.行読み().数に(); 버림 = 数学.商(x, 1); 내림 = 数学.切り下げ(x); 반올림 = 数学.四捨五入(x); コンソール.書く([버림, 내림, 반올림]);}