遊歩道に距離標識を立てる。 最初の標識は出発点から a km の所に立て、その後は s km ごとに一つずつ立てる。 つまり a, a + s, a + 2s, a + 3s, … km のうち b km 以下の所ごとに標識が一つずつある。 a + k × s がちょうど b に等しければ、b km の所にも標識がある。 標識の距離は a + k × s(k = 0, 1, 2, …)を誤差なく正確に計算した値である。 遊歩道ごとに、標識に書く距離を近いものから順に一行に空白一つずつ区切って出力せよ。 距離が整数なら小数点なしで整数として書く(3.00 → 3、0.00 → 0)。 整数でなければ小数点以下をちょうど二桁で書く(2.50 → 2.50、0.05 → 0.05)。 カンマや単位を付けず、数字だけを書く。
入力
一行目に遊歩道の数 q が与えられる。 続く q 行に、遊歩道ごとの a, b, s が空白一つで区切って与えられる。 a, b, s はどれも小数点以下をちょうど二桁で書いた数である(例:0.00、2.50、0.10)。 整数部分の前に余計な 0 は付かない。 1 ≤ q ≤ 100、0.00 ≤ a ≤ b ≤ 10000.00、0.01 ≤ s ≤ 10000.00 すべての遊歩道の標識の数を合わせると 10,000 個以下である。
import sysdef hundredths(text): # "12.34" -> 1234: 점을 빼면 0.01 km 단위의 정수가 된다(실수로 바꾸지 않는다) return int(text.replace(".", ""))def show(v): # 0.01 km 단위 정수 v를 거리 문자열로 if v % 100 == 0: return str(v // 100) return f"{v // 100}.{v % 100:02d}"def main(): lines = sys.stdin.read().split("\n") q = int(lines[0]) result = [] for i in range(1, q + 1): a, b, s = map(hundredths, lines[i].split()) result.append(" ".join(show(v) for v in range(a, b + 1, s))) print("\n".join(result))main()
JavaScriptmain.js · 22 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");// "12.34" → 1234: 점을 빼면 0.01 km 단위의 정수가 된다(실수로 계산하지 않는다)const hundredths = (t) => Number(t.replace(".", ""));// 0.01 km 단위 정수 v를 거리 문자열로function show(v) { const whole = Math.trunc(v / 100); const part = v % 100; if (part === 0) return String(whole); return whole + "." + String(part).padStart(2, "0");}const q = Number(lines[0]);const result = [];for (let i = 1; i <= q; i++) { const [a, b, s] = lines[i].trim().split(" ").map(hundredths); const signs = []; for (let v = a; v <= b; v += s) signs.push(show(v)); result.push(signs.join(" "));}console.log(result.join("\n"));
TypeScriptmain.ts · 22 行
const lines: string[] = require("fs").readFileSync(0, "utf8").split("\n");// "12.34" → 1234: 점을 빼면 0.01 km 단위의 정수가 된다(실수로 계산하지 않는다)const hundredths = (t: string): number => Number(t.replace(".", ""));// 0.01 km 단위 정수 v를 거리 문자열로function show(v: number): string { const whole = Math.trunc(v / 100); const part = v % 100; if (part === 0) return String(whole); return whole + "." + String(part).padStart(2, "0");}const q = Number(lines[0]);const result: string[] = [];for (let i = 1; i <= q; i++) { const [a, b, s] = lines[i].trim().split(" ").map(hundredths); const signs: string[] = []; for (let v = a; v <= b; v += s) signs.push(show(v)); result.push(signs.join(" "));}console.log(result.join("\n"));
JavaMain.java · 32 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException;import java.util.Locale;import java.util.StringJoiner;public class Main { // "12.34" → 1234: 점을 빼면 0.01 km 단위의 정수가 된다(실수로 바꾸지 않는다) static int hundredths(String t) { return Integer.parseInt(t.replace(".", "")); } // 0.01 km 단위 정수 v를 거리 문자열로 static String show(int v) { if (v % 100 == 0) return String.valueOf(v / 100); return String.format(Locale.ROOT, "%d.%02d", v / 100, v % 100); } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(in.readLine().trim()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < q; i++) { String[] p = in.readLine().trim().split(" "); int a = hundredths(p[0]), b = hundredths(p[1]), s = hundredths(p[2]); StringJoiner line = new StringJoiner(" "); for (int v = a; v <= b; v += s) line.add(show(v)); sb.append(line).append('\n'); } System.out.print(sb); }}
C#Main.cs · 28 行
using System;using System.Collections.Generic;using System.Text;class Program{ // "12.34" → 1234: 점을 빼면 0.01 km 단위의 정수가 된다(실수로 바꾸지 않는다) static int Hundredths(string t) => int.Parse(t.Replace(".", "")); // 0.01 km 단위 정수 v를 거리 문자열로 static string Show(int v) => v % 100 == 0 ? (v / 100).ToString() : $"{v / 100}.{v % 100:D2}"; static void Main() { int q = int.Parse(Console.ReadLine().Trim()); var sb = new StringBuilder(); for (int i = 0; i < q; i++) { string[] p = Console.ReadLine().Trim().Split(' '); int a = Hundredths(p[0]), b = Hundredths(p[1]), s = Hundredths(p[2]); var signs = new List<string>(); for (int v = a; v <= b; v += s) signs.Add(Show(v)); sb.Append(string.Join(" ", signs)).Append('\n'); } Console.Write(sb); }}
C++main.cpp · 27 行
#include <iostream>#include <iomanip>#include <string>// "12.34" → 1234: 점을 빼면 0.01 km 단위의 정수가 된다(실수로 바꾸지 않는다)int hundredths(std::string t) { t.erase(t.find('.'), 1); return std::stoi(t);}int main() { std::ios::sync_with_stdio(false); int q; std::cin >> q; for (int i = 0; i < q; i++) { std::string ta, tb, ts; std::cin >> ta >> tb >> ts; int a = hundredths(ta), b = hundredths(tb), s = hundredths(ts); for (int v = a; v <= b; v += s) { if (v > a) std::cout << ' '; std::cout << v / 100; if (v % 100 != 0) std::cout << '.' << std::setw(2) << std::setfill('0') << v % 100; } std::cout << '\n'; } return 0;}
Cmain.c · 25 行
#include <stdio.h>/* "12.34"를 정수 12와 34로 따로 읽는다 — 소수는 정확히 두 자리라 34는 0.01 km 단위다 */int read_hundredths(void) { int whole, part; scanf("%d.%d", &whole, &part); return whole * 100 + part;}int main(void) { int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int a = read_hundredths(); int b = read_hundredths(); int s = read_hundredths(); for (int v = a; v <= b; v += s) { if (v > a) putchar(' '); if (v % 100 == 0) printf("%d", v / 100); else printf("%d.%02d", v / 100, v % 100); } putchar('\n'); } return 0;}
Craykonmain.cray · 35 行
// "12.34" → "1234" → 1234: 점을 빼면 0.01 km 단위의 정수가 된다(실수로 계산하지 않는다)백분(값){ 返す 값.置き換え(".", "").数に();}// 0.01 km 단위 정수 v를 거리 문자열로보이기(v){ 정수부 = 数学.商(v, 100); もし (v % 100 == 0) { 返す "" + 정수부; } 返す 정수부 + "." + ("" + v % 100).前埋め(2, "0");}q = コンソール.行読み().数に();繰り返す (i = 0; i < q; i++){ 칸 = コンソール.行読み().空白除去().分ける(" "); a = 백분(칸[0]); b = 백분(칸[1]); s = 백분(칸[2]); 줄 = ""; 繰り返す (v = a; v <= b; v += s) { もし (v > a) { 줄 += " "; } 줄 += 보이기(v); } コンソール.書く(줄);}