Follow the public account below to register for more group events
Information Literacy Competition Question 3
1.
[Programming Problem]
Question 3: Output Multi-Base Numbers
[Problem Description]
Input a positive integer n less than 20, and output all n-digit m-base numbers in ascending order, with each number on a new line.
[Input Format]
Input a positive integer n less than 20, and a positive integer m less than 10.
[Output Format]
Output all n-digit m-base numbers in ascending order, with each number on a new line.
[Sample Input]
3 2
[Sample Output]
000
001
010
011
100
101
110
111
2.
Reference Answer
Question 3: Output Multi-Base Numbers
1. How to Determine the Range of n-Digit m-Base Numbers
1. Counting Rules for m-Base: The Range of Each Digit
In m-base, each digit can only be from 0 to m-1 (because of “carry over at m”).
For example:
Decimal (m=10): Each digit is 0~9;
Binary (m=2): Each digit is 0~1;
Hexadecimal (m=16): Each digit is 0~1 (0~9, A~F)
2. Limitations of n-Digits: Composition of Minimum and Maximum Numbers
“n digits” means that the number must consist of exactly n digits (if less than n digits, pad with 0 at the front).
(1) The smallest n-digit m-base number: 00…0 (n zeros)
When all digits take the minimum value 0, this number is the smallest n-digit m-base number, which is 000…0 (n zeros in total).
The corresponding decimal value: 0 (since any number multiplied by 0 is still 0, and the sum remains 0).
(2) The largest n-digit m-base number: (m-1)(m-1)…(m-1) (n times m-1)
When all digits take the maximum value (m-1), this number is the largest n-digit m-base number. For example:
The maximum value of a 3-digit decimal number is 999 (each digit is 10-1=9);
The maximum value of a 2-digit binary number is 11 (each digit is 2-1=1).
The corresponding decimal value: The weight of each digit (from right to left, from the 1st to the nth digit) is m⁰, m¹, …, mⁿ⁻¹, so the total is: (m-1)×m⁰ + (m-1)×m¹ + … + (m-1)×mⁿ⁻¹. Factoring out (m-1) and using the geometric series sum formula: (m-1) × (mⁿ – 1)/(m – 1) = mⁿ – 1
Example Verification
For “2-digit binary number” (n=2, m=2):
The smallest number is 00 (binary) → corresponding decimal 0;
The largest number is 11 (binary) → corresponding decimal 3 (1×2¹ + 1×2⁰ = 3);
And mⁿ – 1 = 2² – 1 = 3, which matches perfectly.
2. Code Logic Flow

3. Code Reference




Follow the public account below to register for more group events