Integers n and s are given. Starting at n and decreasing by s each time, print only the numbers that are at least 1, all on one line. Put exactly one space between numbers and no space at the start of the line. For example, n = 10, s = 3 gives 10 7 4 1, and n = 10, s = 5 gives 10 5 (0 is not printed).
Input
1 ≤ s ≤ n ≤ 10,000
Output
one line
Example 1 · stdin
10 3
stdout
10 7 4 1
Example 2 · stdin
10 5
stdout
10 5
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
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
Where to stop is the trap. Python range(n, 0, -s) excludes the end (0) and so reaches 1; writing range(n, 1, -s) drops 1 even when the sequence hits it. In C-style for (i = n; i >= 1; i -= s), writing i >= 0 prints 0 whenever n is a multiple of s. If you count iterations instead, the count is (n - 1) / s + 1, not n / s (10 3 has 4 numbers). Put spaces only between numbers: prefixing every number with " " starts the line with a space, which is wrong (this judge only forgives spaces at the end of a line). Most languages have a join tool: Python " ".join(...) or print(*range(n, 0, -s)), JS/TS array.join(" "), Java StringJoiner / String.join, C# string.Join(" ", …). C and C++ (before C++23) have no handy one, so write "space unless it is the first number" yourself. Craykon works like C: write with Console.Write(…) (no newline) and end the line with Console.WriteLine("") (collecting into a list and using list.Join(" ") also works); the backward step is for (i = n; i >= 1; i -= s).
Pythonmain.py · 3 lines
n, s = map(int, input().split())# range(시작, 끝, 걸음): 끝(0)은 들어가지 않는다 → 1까지print(" ".join(str(i) for i in range(n, 0, -s)))
JavaScriptmain.js · 4 lines
const [n, s] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = [];for (let i = n; i >= 1; i -= s) out.push(i);console.log(out.join(" "));
TypeScriptmain.ts · 5 lines
import { readFileSync } from "node:fs";const [n, s]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: number[] = [];for (let i = n; i >= 1; i -= s) out.push(i);console.log(out.join(" "));
JavaMain.java · 12 lines
import java.util.Scanner;import java.util.StringJoiner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), s = sc.nextInt(); StringJoiner out = new StringJoiner(" "); for (int i = n; i >= 1; i -= s) out.add(String.valueOf(i)); System.out.println(out); }}
C#Main.cs · 14 lines
using System;using System.Collections.Generic;class Program{ static void Main() { int[] v = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); int n = v[0], s = v[1]; var out_ = new List<int>(); for (int i = n; i >= 1; i -= s) out_.Add(i); Console.WriteLine(string.Join(" ", out_)); }}
C++main.cpp · 12 lines
#include <iostream>int main() { std::ios::sync_with_stdio(false); int n, s; std::cin >> n >> s; for (int i = n; i >= 1; i -= s) { if (i != n) std::cout << ' '; std::cout << i; } std::cout << "\n";}
Cmain.c · 12 lines
#include <stdio.h>int main(void) { int n, s; scanf("%d %d", &n, &s); for (int i = n; i >= 1; i -= s) { if (i != n) putchar(' '); /* 사이에만 빈칸: 첫 수 앞에는 없음 */ printf("%d", i); } putchar('\n'); return 0;}
Craykonmain.cray · 12 lines
v = Console.ReadNumbers();n = v[0];s = v[1];for (i = n; i >= 1; i -= s){ if (i != n) { Console.Write(" "); } Console.Write(i);}Console.WriteLine("");