📌  相关文章
📜  具有唯一(或不同)数字的数字

📅  最后修改于: 2021-10-27 08:10:36             🧑  作者: Mango

给定一个范围,打印所有具有唯一数字的数字。

例子 :

Input : 10 20
Output : 10 12 13 14 15 16 17 18 19 20  (Except 11)

Input : 1 10
Output : 1 2 3 4 5 6 7 8 9 10

方法:

As the problem is pretty simple, the only thing to be done is :-
1- Find the digits one by one and keep marking visited digits.
2- If all digits occurs one time only then print that number.
3- Else not.
C++
// C++ implementation to find unique digit
// numbers in a range
#include
using namespace std;
 
// Function to print unique digit numbers
// in range from l to r.
void printUnique(int l, int r)
{
    // Start traversing the numbers
    for (int i=l ; i<=r ; i++)
    {
        int num = i;
        bool visited[10] = {false};
 
        // Find digits and maintain its hash
        while (num)
        {
            // if a digit occurs more than 1 time
            // then break
            if (visited[num % 10])
                break;
 
            visited[num%10] = true;
 
            num = num/10;
        }
 
        // num will be 0 only when above loop
        // doesn't get break that means the
        // number is unique so print it.
        if (num == 0)
            cout << i << " ";
    }
}
 
// Driver code
int main()
{
    int l = 1, r = 20;
    printUnique(l, r);
    return 0;
}


Java
// Java implementation to find unique digit
// numbers in a range
class Test
{
    // Method to print unique digit numbers
    // in range from l to r.
    static void printUnique(int l, int r)
    {
        // Start traversing the numbers
        for (int i=l ; i<=r ; i++)
        {
            int num = i;
            boolean visited[] = new boolean[10];
      
            // Find digits and maintain its hash
            while (num != 0)
            {
                // if a digit occurs more than 1 time
                // then break
                if (visited[num % 10])
                    break;
      
                visited[num%10] = true;
      
                num = num/10;
            }
      
            // num will be 0 only when above loop
            // doesn't get break that means the
            // number is unique so print it.
            if (num == 0)
                System.out.print(i + " ");
        }
    }
     
    // Driver method
    public static void main(String args[])
    {
        int l = 1, r = 20;
        printUnique(l, r);
    }
}


Python3
# Python3 implementation
# to find unique digit
# numbers in a range
 
# Function to print
# unique digit numbers
# in range from l to r.
def printUnique(l,r):
     
    # Start traversing
    # the numbers
    for i in range (l, r + 1):
        num = i;
        visited = [0,0,0,0,0,0,0,0,0,0];
         
        # Find digits and
        # maintain its hash
        while (num):
             
            # if a digit occurs
            # more than 1 time
            # then break
            if visited[num % 10] == 1:
                break;
            visited[num % 10] = 1;
            num = (int)(num / 10);
             
        # num will be 0 only when
        # above loop doesn't get
        # break that means the
        # number is unique so
        # print it.
        if num == 0:
            print(i, end = " ");
 
# Driver code
l = 1;
r = 20;
printUnique(l, r);
 
# This code is
# contributed by mits


C#
// C# implementation to find unique digit
// numbers in a range
using System;
         
public class GFG {
     
    // Method to print unique digit numbers
    // in range from l to r.
    static void printUnique(int l, int r)
    {
         
        // Start traversing the numbers
        for (int i = l ; i <= r ; i++)
        {
            int num = i;
            bool []visited = new bool[10];
     
            // Find digits and maintain
            // its hash
            while (num != 0)
            {
                 
                // if a digit occurs more
                // than 1 time then break
                if (visited[num % 10])
                    break;
     
                visited[num % 10] = true;
     
                num = num / 10;
            }
     
            // num will be 0 only when
            // above loop doesn't get
            // break that means the number
            // is unique so print it.
            if (num == 0)
                Console.Write(i + " ");
        }
    }
     
    // Driver method
    public static void Main()
    {
        int l = 1, r = 20;
        printUnique(l, r);
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


C++
int n;
cin>>n;
string s = to_string(n);


C++
set uniDigits(s.begin(), s.end());


C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to print unique
// numbers
void printUnique(int l, int r){
   
  // Iterate from l to r
  for (int i = l; i <= r; i++) {
       
        // Convert the no. to
        // string
        string s = to_string(i);
       
        // Convert string to set using stl
        set uniDigits(s.begin(), s.end());
       
        // Output if condition satisfies
        if (s.size() == uniDigits.size()) {
            cout << i << " ";
        }
    }
}
 
// Driver Code
int main()
{
   
    // Input of the lower and
    // higher limits
    int l = 1, r = 20;
     
    // Function Call
    printUnique(l, r);
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG{
 
// Function to print unique
// numbers
static void printUnique(int l, int r)
{
     
    // Iterate from l to r
    for(int i = l; i <= r; i++)
    {
         
        // Convert the no. to
        // String
        String s = String.valueOf(i);
       
        // Convert String to set using stl
        HashSet uniDigits = new HashSet();
        for(int c : s.toCharArray())
            uniDigits.add(c);
             
        // Output if condition satisfies
        if (s.length() == uniDigits.size())
        {
            System.out.print(i+ " ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Input of the lower and
    // higher limits
    int l = 1, r = 20;
     
    // Function Call
    printUnique(l, r);
}
}
 
// This code is contributed by Princi Singh


C#
// C# code for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to print unique
// numbers
static void printUnique(int l, int r)
{
     
    // Iterate from l to r
    for(int i = l; i <= r; i++)
    {
         
        // Convert the no. to
        // String
        String s = String.Join("", i);
       
        // Convert String to set using stl
        HashSet uniDigits = new HashSet();
        foreach(int c in s.ToCharArray())
            uniDigits.Add(c);
             
        // Output if condition satisfies
        if (s.Length == uniDigits.Count)
        {
            Console.Write(i + " ");
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
   
    // Input of the lower and
    // higher limits
    int l = 1, r = 20;
     
    // Function Call
    printUnique(l, r);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:

1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20

另一种方法:

使用 set STL 来检查一个数字是否只有唯一的数字。然后我们可以比较由给定数字和新创建的集合形成的字符串s 的大小。例如,让我们考虑数字 1987,然后我们可以将数字转换为字符串,

C++

int n;
cin>>n;
string s = to_string(n);

之后,用字符串s 的内容初始化一个集合。

C++

set uniDigits(s.begin(), s.end());

然后我们可以比较字符串s 和新创建的集合 uniDigits 的大小。

以下是上述方法的代码:

C++

// C++ code for the above approach
#include 
using namespace std;
 
// Function to print unique
// numbers
void printUnique(int l, int r){
   
  // Iterate from l to r
  for (int i = l; i <= r; i++) {
       
        // Convert the no. to
        // string
        string s = to_string(i);
       
        // Convert string to set using stl
        set uniDigits(s.begin(), s.end());
       
        // Output if condition satisfies
        if (s.size() == uniDigits.size()) {
            cout << i << " ";
        }
    }
}
 
// Driver Code
int main()
{
   
    // Input of the lower and
    // higher limits
    int l = 1, r = 20;
     
    // Function Call
    printUnique(l, r);
    return 0;
}

Java

// Java code for the above approach
import java.util.*;
 
class GFG{
 
// Function to print unique
// numbers
static void printUnique(int l, int r)
{
     
    // Iterate from l to r
    for(int i = l; i <= r; i++)
    {
         
        // Convert the no. to
        // String
        String s = String.valueOf(i);
       
        // Convert String to set using stl
        HashSet uniDigits = new HashSet();
        for(int c : s.toCharArray())
            uniDigits.add(c);
             
        // Output if condition satisfies
        if (s.length() == uniDigits.size())
        {
            System.out.print(i+ " ");
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Input of the lower and
    // higher limits
    int l = 1, r = 20;
     
    // Function Call
    printUnique(l, r);
}
}
 
// This code is contributed by Princi Singh

C#

// C# code for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to print unique
// numbers
static void printUnique(int l, int r)
{
     
    // Iterate from l to r
    for(int i = l; i <= r; i++)
    {
         
        // Convert the no. to
        // String
        String s = String.Join("", i);
       
        // Convert String to set using stl
        HashSet uniDigits = new HashSet();
        foreach(int c in s.ToCharArray())
            uniDigits.Add(c);
             
        // Output if condition satisfies
        if (s.Length == uniDigits.Count)
        {
            Console.Write(i + " ");
        }
    }
}
 
// Driver Code
public static void Main(String[] args)
{
   
    // Input of the lower and
    // higher limits
    int l = 1, r = 20;
     
    // Function Call
    printUnique(l, r);
}
}
 
// This code is contributed by Princi Singh

Javascript


输出
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20