Distance signs are put up along a walking trail. The first sign stands a km from the start, and after that one sign stands every s km. So there is one sign at each of a, a + s, a + 2s, a + 3s, … km that is at most b km. If a + k × s is exactly b, there is a sign at b km too. The distance on a sign is a + k × s (k = 0, 1, 2, …) computed exactly, without any error. For each trail, print the distances on its signs from nearest to farthest on one line, separated by single spaces. If a distance is a whole number, write it as an integer without a decimal point (3.00 → 3, 0.00 → 0). Otherwise write exactly two digits after the decimal point (2.50 → 2.50, 0.05 → 0.05). Write plain digits only, with no commas or units.
Input
The first line gives the number of trails q. Each of the next q lines gives a, b, s for one trail, separated by single spaces. a, b, s are all written with exactly two digits after the decimal point (e.g. 0.00, 2.50, 0.10). The integer part has no extra leading zeros. 1 ≤ q ≤ 100, 0.00 ≤ a ≤ b ≤ 10000.00, 0.01 ≤ s ≤ 10000.00 The total number of signs over all trails is at most 10,000.
Output
One line per trail, in input order. Since a ≤ b, every trail has at least one sign.
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
Computing the distances as floating-point doubles is wrong. 0.1 cannot be written exactly in binary, and Python float, JS/TS number, Java/C#/C/C++ double and Craykon numbers are all the same IEEE 754 double, so all 8 languages go wrong the same way. Trap one: accumulating with x += s. Adding 0.1 three times gives 0.30000000000000004, which is past b = 0.30, so the 0.30 sign is lost; adding it ten times gives 0.9999999999999999, so x == floor(x) is false and 1 is written as 1.00 (lines one and three of the example). Craykon Console.WriteLine(x) rounds to 9 decimal places and shows this value as 1, but x == 1 is false: the error is only hidden. Trap two: computing a + k * s fresh each time instead of accumulating still fails, because 3 * 0.1 is 0.30000000000000004 and the same sign is lost. Trap three: a tolerance x <= b + 1e-9. It works on small cases, but the error builds up: adding 0.01 to 9900.01 9,999 times gives 10000.000000002183, so 1e-9 is not enough and the last sign, 10000, is lost. Trap four: reading as a double, multiplying by 100 and cutting off. 0.29 * 100 is 28.999999999999996, so (int) in C/C++/Java/C#, Python int() and JS Math.trunc give 28 and the spacing becomes 0.28. The safe way is to read whole hundredths of a km from the start. There are always exactly two decimals, so just drop the point ("12.34" → 1234): Python int(t.replace(".", "")), JS/TS Number(t.replace(".", "")), Java Integer.parseInt(t.replace(".", "")), C# int.Parse(t.Replace(".", "")), C scanf("%d.%d", &whole, &part), C++ t.erase(t.find('.'), 1) then std::stoi, Craykon t.Replace(".", "").ToNumber(). Then the signs are an integer loop for (v = a; v <= b; v += s), and the largest v is two million, so a 32-bit int is enough. To print, write the integer part v / 100 and the two digits v % 100 separately. In JS/TS and Craykon / divides into decimals, so use Math.trunc(v / 100) and Mathf.Quot(v, 100). Zero-padding the two digits: Python f"{v % 100:02d}", C printf("%d.%02d", ...), C++ std::setw(2) << std::setfill('0'), Java String.format(Locale.ROOT, "%d.%02d", ...), C# {v % 100:D2}, JS/TS padStart(2, "0"), Craykon PadLeft(2, "0"). Without the padding, 0.05 comes out as 0.5.
Pythonmain.py · 26 lines
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 lines
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 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
// "12.34" → "1234" → 1234: 점을 빼면 0.01 km 단위의 정수가 된다(실수로 계산하지 않는다)백분(값){ return 값.Replace(".", "").ToNumber();}// 0.01 km 단위 정수 v를 거리 문자열로보이기(v){ 정수부 = Mathf.Quot(v, 100); if (v % 100 == 0) { return "" + 정수부; } return 정수부 + "." + ("" + v % 100).PadLeft(2, "0");}q = Console.ReadLine().ToNumber();for (i = 0; i < q; i++){ 칸 = Console.ReadLine().Trim().Split(" "); a = 백분(칸[0]); b = 백분(칸[1]); s = 백분(칸[2]); 줄 = ""; for (v = a; v <= b; v += s) { if (v > a) { 줄 += " "; } 줄 += 보이기(v); } Console.WriteLine(줄);}