📌  相关文章
📜  打印所有小于N的数字,最多2个唯一数字

📅  最后修改于: 2021-05-04 14:13:57             🧑  作者: Mango

给定数字N(小于10 ^ 9)。任务是打印所有小于N的数字,这些数字最多包含2个唯一数字。

注意:诸如100、111、101之类的数字有效,因为唯一数字的位数最多为2,但是123无效,因为它具有3个唯一数字。

例子:

方法

  • 因为我们说最多两个唯一数字,所以两个循环将使我们全部由两个数字组成。
  • 递归函数用术语来表示,以使用两位数字生成所有数字。
  • 调用num最初为0的函数,并使用递归num * 10 + anum * 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 )