给定数字N(小于10 ^ 9)。任务是打印所有小于N的数字,这些数字最多包含2个唯一数字。
注意:诸如100、111、101之类的数字有效,因为唯一数字的位数最多为2,但是123无效,因为它具有3个唯一数字。
例子:
Input: N = 12
Output: The numbers are: 1 2 3 4 5 6 7 8 9 10 11
Input: N = 154
Output: The numbers are: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 110 111 112 113 114 115 116 117 118 119 121 122 131 133 141 144 151
方法:
- 因为我们说最多两个唯一数字,所以两个循环将使我们全部由两个数字组成。
- 递归函数是用两位数字生成所有数字的术语。
- 调用num最初为0的函数,并使用递归num * 10 + a和num * 10 + b生成数字,基本情况为num> = n。
- 一个集合可用于存储所有数字,因为递归函数生成重复的元素。
下面是上述方法的实现:
C++
// C++ program to print all the numbers
// less than N which have at most 2 unique digits
#include
using namespace std;
set st;
// Function to generate all possible numbers
void generateNumbers(int n, int num, int a, int b)
{
// If the number is less than n
if (num > 0 && num < n)
st.insert(num);
// If the number exceeds
if (num >= n)
return;
// Check if it is not the same number
if (num * 10 + a > num)
generateNumbers(n, num * 10 + a, a, b);
generateNumbers(n, num * 10 + b, a, b);
}
// Function to print all numbers
void printNumbers(int n)
{
// All combination of digits
for (int i = 0; i <= 9; i++)
for (int j = i + 1; j <= 9; j++)
generateNumbers(n, 0, i, j);
cout << "The numbers are: ";
// Print all numbers
while (!st.empty()) {
cout << *st.begin() << " ";
st.erase(st.begin());
}
}
// Driver code
int main()
{
int n = 12;
printNumbers(n);
return 0;
}
Java
// Java program to print all the numbers
// less than N which have at most 2 unique digits
import java.util.*;
class GFG{
static Set st= new HashSet();
// Function to generate all possible numbers
static void generateNumbers(int n, int num, int a, int b)
{
// If the number is less than n
if (num > 0 && num < n)
st.add(num);
// If the number exceeds
if (num >= n)
return;
// Check if it is not the same number
if (num * 10 + a > num)
generateNumbers(n, num * 10 + a, a, b);
generateNumbers(n, num * 10 + b, a, b);
}
// Function to print all numbers
static void printNumbers(int n)
{
// All combination of digits
for (int i = 0; i <= 9; i++)
for (int j = i + 1; j <= 9; j++)
generateNumbers(n, 0, i, j);
System.out.print( "The numbers are: ");
// Print all numbers
System.out.print(st);
st.clear();
}
// Driver code
public static void main(String args[])
{
int n = 12;
printNumbers(n);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python 3 program to print all the
# numbers less than N which have at
# most 2 unique digits
st = set()
# Function to generate all possible numbers
def generateNumbers(n, num, a, b):
# If the number is less than n
if (num > 0 and num < n):
st.add(num)
# If the number exceeds
if (num >= n):
return
# Check if it is not the same number
if (num * 10 + a > num):
generateNumbers(n, num * 10 + a, a, b)
generateNumbers(n, num * 10 + b, a, b)
# Function to print all numbers
def printNumbers(n):
# All combination of digits
for i in range(10):
for j in range(i + 1, 10, 1):
generateNumbers(n, 0, i, j)
print("The numbers are:", end = " ")
# Print all numbers
l = list(st)
for i in l:
print(i, end = " ")
# Driver code
if __name__ == '__main__':
n = 12
printNumbers(n)
# This code is contributed by
# Shashank_Sharma
C#
// C# program to print all the numbers
// less than N which have at most 2 unique digits
using System;
using System.Collections.Generic;
class GFG
{
static SortedSet st = new SortedSet();
// Function to generate all possible numbers
static void generateNumbers(int n, int num, int a, int b)
{
// If the number is less than n
if (num > 0 && num < n)
st.Add(num);
// If the number exceeds
if (num >= n)
return;
// Check if it is not the same number
if (num * 10 + a > num)
generateNumbers(n, num * 10 + a, a, b);
generateNumbers(n, num * 10 + b, a, b);
}
// Function to print all numbers
static void printNumbers(int n)
{
// All combination of digits
for (int i = 0; i <= 9; i++)
for (int j = i + 1; j <= 9; j++)
generateNumbers(n, 0, i, j);
Console.Write( "The numbers are: ");
// Print all numbers
foreach(int obj in st)
Console.Write(obj+" ");
st.Clear();
}
// Driver code
public static void Main(String []args)
{
int n = 12;
printNumbers(n);
}
}
// This code has been contributed by 29AjayKumar
The numbers are: 1 2 3 4 5 6 7 8 9 10 11
时间复杂度: O(36 * 2 30 )
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。