📜  第 n 个数字在 {0, 1, 2, 3, 4, 5} 中

📅  最后修改于: 2021-09-17 16:10:07             🧑  作者: Mango

给定一个数字 n,我们必须找到第 n 个数字,使其数字仅包含 0、1、2、3、4 或 5。

例子 :

Input: n = 6
Output: 5

Input:  n = 10
Output: 13

我们首先将 0, 1, 2, 3, 4, 5 存储在一个数组中。我们可以看到接下来的数字将是 10, 11, 12,,13, 14, 15 之后的数字将是 20, 21, 23, 24, 25 等等。我们可以看到一次又一次重复的模式。我们保存计算结果并将其用于进一步计算。
接下来的 6 个数字是-
1*10+0 = 10
1*10+1 = 11
1*10+2 = 12
1*10+3 = 13
1*10+4 = 14
1*10+5 = 15
之后接下来的6个数字将是-
2*10+0 = 20
2*10+1 = 21
2*10+2 = 22
2*10+3 = 23
2*10+4 = 24
2*10+5 = 25
我们使用这种模式来找到第 n 个数字。下面是完整的算法。

1) push 0 to 5 in ans vector
2) for i=0 to n/6
     for j=0 to 6  
          // this will be the case when first 
          // digit will be zero 
          if (ans[i]*10! = 0) 
              ans.push_back(ans[i]*10 + ans[j])
3) print ans[n-1]
C++
// C++ program to find n-th number with digits
// in {0, 1, 2, 3, 4, 5}
#include 
using namespace std;
 
// Returns the N-th number with given digits
int findNth(int n)
{
    // vector to store results
    vector ans;
 
    // push first 6 numbers in the answer
    for (int i = 0; i < 6; i++)
        ans.push_back(i);
 
    // calculate further results
    for (int i = 0; i <= n / 6; i++)
        for (int j = 0; j < 6; j++)
            if ((ans[i] * 10) != 0)
                ans.push_back(ans[i]
                              * 10 + ans[j]);
 
    return ans[n - 1];
}
 
// Driver code
int main()
{
    int n = 10;
    cout << findNth(n);
    return 0;
}


Java
// Java program to find n-th number with digits
// in {0, 1, 2, 3, 4, 5}
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Returns the N-th number with given digits
  public static int findNth(int n)
  {
    // vector to store results
    ArrayList ans = new ArrayList();
 
    // push first 6 numbers in the answer
    for (int i = 0; i < 6; i++)
      ans.add(i);
 
    // calculate further results
    for (int i = 0; i <= n / 6; i++)
      for (int j = 0; j < 6; j++)
        if ((ans.get(i) * 10) != 0)
          ans.add(ans.get(i) * 10 + ans.get(j));
    return ans.get(n - 1);
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int n = 10;
    int ans = findNth(n);
    System.out.println(ans);
  }
}
 
// This code is contributed by RohitOberoi.


Python3
# Python3 program to find n-th number with digits
# in {0, 1, 2, 3, 4, 5}
 
# Returns the N-th number with given digits
def findNth(n):
 
    # vector to store results
    ans = []
 
    # push first 6 numbers in the answer
    for i in range(6):
        ans.append(i)
 
    # calculate further results
    for i in range(n // 6 + 1):
        for j in range(6):
            if ((ans[i] * 10) != 0):
                ans.append(ans[i]
                           * 10 + ans[j])
 
    return ans[n - 1]
 
# Driver code
if __name__ == "__main__":
 
    n = 10
    print(findNth(n))
 
    # This code is contributed by ukasp.


C#
// C# program to find n-th number with digits
// in {0, 1, 2, 3, 4, 5}
using System;
using System.Collections.Generic;
 
class GFG{
 
// Returns the N-th number with given digits
public static int findNth(int n)
{
     
    // Vector to store results
    List ans = new List();
 
    // Push first 6 numbers in the answer
    for(int i = 0; i < 6; i++)
        ans.Add(i);
 
    // Calculate further results
    for(int i = 0; i <= n / 6; i++)
        for(int j = 0; j < 6; j++)
            if ((ans[i] * 10) != 0)
                ans.Add(ans[i] * 10 + ans[j]);
                 
    return ans[n - 1];
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 10;
    int ans = findNth(n);
     
    Console.WriteLine(ans);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


C++
// CPP code to find nth number
// with digits 0, 1, 2, 3, 4, 5
#include 
using namespace std;
 
#define max 100000
 
// function to convert num to base 6
int baseconversion(int arr[], int num, int base)
 
{
    int i = 0, rem, j;
 
    if (num == 0) {
        return 0;
    }
 
    while (num > 0) {
        rem = num % base;
 
        arr[i++] = rem;
 
        num /= base;
    }
 
    return i;
}
 
// Driver code
int main()
{
 
    // initialize an array to 0
    int arr[max] = { 0 };
 
    int n = 10;
 
    // function calling to convert
    // number n to base 6
    int size = baseconversion(arr, n - 1, 6);
 
    // if size is zero then return zero
    if (size == 0)
 
        cout << size;
 
    for (int i = size - 1; i >= 0; i--) {
 
        cout << arr[i];
    }
 
    return 0;
}
 
// Code is contributed by Anivesh Tiwari.


Java
// Java code to find nth number
// with digits 0, 1, 2, 3, 4, 5
class GFG {
     
    static final int max = 100000;
     
    // function to convert num to base 6
    static int baseconversion(int arr[],
                          int num, int base)
    {
        int i = 0, rem, j;
     
        if (num == 0) {
            return 0;
        }
     
        while (num > 0) {
             
            rem = num % base;
            arr[i++] = rem;
            num /= base;
        }
     
        return i;
    }
     
    // Driver code
    public static void main (String[] args)
    {
         
        // initialize an array to 0
        int arr[] = new int[max];
     
        int n = 10;
     
        // function calling to convert
        // number n to base 6
        int size = baseconversion(arr, n - 1, 6);
     
        // if size is zero then return zero
        if (size == 0)
            System.out.print(size);
     
        for (int i = size - 1; i >= 0; i--) {
            System.out.print(arr[i]);
        }
    }
}
 
// This code is contributed by Anant Agarwal.


C#
// C# code to find nth number
// with digits 0, 1, 2, 3, 4, 5
using System;
 
class GFG {
     
    static int max = 100000;
     
    // function to convert num to base 6
    static int baseconversion(int []arr,
                              int num, int bas)
    {
        int i = 0, rem;
     
        if (num == 0) {
            return 0;
        }
     
        while (num > 0) {
             
            rem = num % bas;
            arr[i++] = rem;
            num /= bas;
        }
     
        return i;
    }
     
    // Driver code
    public static void Main ()
    {
        // initialize an array to 0
        int []arr = new int[max];
     
        int n = 10;
     
        // function calling to convert
        // number n to base 6
        int size = baseconversion(arr, n - 1, 6);
     
        // if size is zero then return zero
        if (size == 0)
            Console.Write(size);
     
        for (int i = size - 1; i >= 0; i--) {
            Console.Write(arr[i]);
        }
    }
}
 
// This code is contributed by nitin mittal


Javascript


C++
// C++ code to find nth number
// with digits 0, 1, 2, 3, 4, 5
 
#include 
using namespace std;
 
int ans(int n){
  // If the Number is less than 6 return the number as it is.
  if(n < 6){
    return n;
  }
  //Call the function again and again the get the desired result.
  //And convert the number to base 6.
  return n%6 + 10*(ans(n/6));
}
 
int getSpecialNumber(int N)
{
  //Decrease the Number by 1 and Call ans function
  // to convert N to base 6
  return ans(--N);
}
 
/*Example:-
Input: N = 17
Output: 24
 
Explaination:-
decrease 17 by 1
N = 16
call ans() on 16
 
ans():
    16%6 + 10*(ans(16/6))
        since 16/6 = 2 it is less than 6 the ans returns value as it is.
    4 + 10*(2)
    = 24
 
hence answer is 24.*/
 
int main()
{
  int N = 17;
  int answer = getSpecialNumber(N);
  cout<


Java
// Java code to find nth number
// with digits 0, 1, 2, 3, 4, 5
import java.util.*;
 
class GFG{
 
static int ans(int n)
{
     
    // If the Number is less than 6 return
    // the number as it is.
    if (n < 6)
    {
        return n;
    }
     
    // Call the function again and again
    // the get the desired result.
    // And convert the number to base 6.
    return n % 6 + 10 * (ans(n / 6));
}
 
static int getSpecialNumber(int N)
{
     
    // Decrease the Number by 1 and Call
    // ans function to convert N to base 6
    return ans(--N);
}
 
/*
 * Example:- Input: N = 17 Output: 24
 *
 * Explaination:- decrease 17 by 1 N = 16 call ans() on 16
 *
 * ans(): 16%6 + 10*(ans(16/6)) since 16/6 = 2 it is less
 * than 6 the ans returns value as it is. 4 + 10*(2) = 24
 *
 * hence answer is 24.
 */
// Driver code
public static void main(String[] args)
{
    int N = 17;
    int answer = getSpecialNumber(N);
     
    System.out.println(answer);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 code to find nth number
# with digits 0, 1, 2, 3, 4, 5
def ans(n):
 
    # If the Number is less than 6 return
    # the number as it is.
    if (n < 6):
        return n
     
    # Call the function again and again
    # the get the desired result.
    # And convert the number to base 6.
    return n % 6 + 10 * (ans(n // 6)) - 1
 
def getSpecialNumber(N):
     
    # Decrease the Number by 1 and Call
    # ans function to convert N to base 6
    return ans(N)
 
'''
 * Example:- Input: N = 17 Output: 24
 *
 * Explaination:- decrease 17 by 1 N = 16 call ans() on 16
 *
 * ans(): 16%6 + 10*(ans(16/6)) since 16/6 = 2 it is less than 6 the ans returns
 * value as it is. 4 + 10*(2) = 24
 *
 * hence answer is 24.
 '''
# Driver code
if __name__ == '__main__':
     
    N = 17
    answer = getSpecialNumber(N)
 
    print(answer)
 
# This code contributed by aashish1995


C#
// C# code to find nth number
// with digits 0, 1, 2, 3, 4, 5
using System;
 
public class GFG{
 
static int ans(int n)
{
     
    // If the Number is less than 6 return
    // the number as it is.
    if (n < 6)
    {
        return n;
    }
     
    // Call the function again and again
    // the get the desired result.
    // And convert the number to base 6.
    return n % 6 + 10 * (ans(n / 6));
}
 
static int getSpecialNumber(int N)
{
     
    // Decrease the Number by 1 and Call
    // ans function to convert N to base 6
    return ans(--N);
}
 
/*
 * Example:- Input: N = 17 Output: 24
 *
 * Explaination:- decrease 17 by 1 N = 16 call ans() on 16
 *
 * ans(): 16%6 + 10*(ans(16/6)) since 16/6 = 2 it is less
 * than 6 the ans returns value as it is. 4 + 10*(2) = 24
 *
 * hence answer is 24.
 */
// Driver code
public static void Main(String[] args)
{
    int N = 17;
    int answer = getSpecialNumber(N);
     
    Console.WriteLine(answer);
}
}
 
 
// This code is contributed by Rajput-Ji


Javascript


输出
13

有效方法:

算法 :

  1. 首先将数字 n 转换为基数 6。
  2. 将转换后的值同时存储在一个数组中。
  3. 以相反的顺序打印该数组。

下面是上述算法的实现:

C++

// CPP code to find nth number
// with digits 0, 1, 2, 3, 4, 5
#include 
using namespace std;
 
#define max 100000
 
// function to convert num to base 6
int baseconversion(int arr[], int num, int base)
 
{
    int i = 0, rem, j;
 
    if (num == 0) {
        return 0;
    }
 
    while (num > 0) {
        rem = num % base;
 
        arr[i++] = rem;
 
        num /= base;
    }
 
    return i;
}
 
// Driver code
int main()
{
 
    // initialize an array to 0
    int arr[max] = { 0 };
 
    int n = 10;
 
    // function calling to convert
    // number n to base 6
    int size = baseconversion(arr, n - 1, 6);
 
    // if size is zero then return zero
    if (size == 0)
 
        cout << size;
 
    for (int i = size - 1; i >= 0; i--) {
 
        cout << arr[i];
    }
 
    return 0;
}
 
// Code is contributed by Anivesh Tiwari.

Java

// Java code to find nth number
// with digits 0, 1, 2, 3, 4, 5
class GFG {
     
    static final int max = 100000;
     
    // function to convert num to base 6
    static int baseconversion(int arr[],
                          int num, int base)
    {
        int i = 0, rem, j;
     
        if (num == 0) {
            return 0;
        }
     
        while (num > 0) {
             
            rem = num % base;
            arr[i++] = rem;
            num /= base;
        }
     
        return i;
    }
     
    // Driver code
    public static void main (String[] args)
    {
         
        // initialize an array to 0
        int arr[] = new int[max];
     
        int n = 10;
     
        // function calling to convert
        // number n to base 6
        int size = baseconversion(arr, n - 1, 6);
     
        // if size is zero then return zero
        if (size == 0)
            System.out.print(size);
     
        for (int i = size - 1; i >= 0; i--) {
            System.out.print(arr[i]);
        }
    }
}
 
// This code is contributed by Anant Agarwal.

C#

// C# code to find nth number
// with digits 0, 1, 2, 3, 4, 5
using System;
 
class GFG {
     
    static int max = 100000;
     
    // function to convert num to base 6
    static int baseconversion(int []arr,
                              int num, int bas)
    {
        int i = 0, rem;
     
        if (num == 0) {
            return 0;
        }
     
        while (num > 0) {
             
            rem = num % bas;
            arr[i++] = rem;
            num /= bas;
        }
     
        return i;
    }
     
    // Driver code
    public static void Main ()
    {
        // initialize an array to 0
        int []arr = new int[max];
     
        int n = 10;
     
        // function calling to convert
        // number n to base 6
        int size = baseconversion(arr, n - 1, 6);
     
        // if size is zero then return zero
        if (size == 0)
            Console.Write(size);
     
        for (int i = size - 1; i >= 0; i--) {
            Console.Write(arr[i]);
        }
    }
}
 
// This code is contributed by nitin mittal

Javascript


输出:

13

另一种有效的方法:

算法:

  1. 将数字 N 减少 1。
  2. 2. 将数字 N 转换为基数 6。

下面是上述算法的实现:

C++

// C++ code to find nth number
// with digits 0, 1, 2, 3, 4, 5
 
#include 
using namespace std;
 
int ans(int n){
  // If the Number is less than 6 return the number as it is.
  if(n < 6){
    return n;
  }
  //Call the function again and again the get the desired result.
  //And convert the number to base 6.
  return n%6 + 10*(ans(n/6));
}
 
int getSpecialNumber(int N)
{
  //Decrease the Number by 1 and Call ans function
  // to convert N to base 6
  return ans(--N);
}
 
/*Example:-
Input: N = 17
Output: 24
 
Explaination:-
decrease 17 by 1
N = 16
call ans() on 16
 
ans():
    16%6 + 10*(ans(16/6))
        since 16/6 = 2 it is less than 6 the ans returns value as it is.
    4 + 10*(2)
    = 24
 
hence answer is 24.*/
 
int main()
{
  int N = 17;
  int answer = getSpecialNumber(N);
  cout<

Java

// Java code to find nth number
// with digits 0, 1, 2, 3, 4, 5
import java.util.*;
 
class GFG{
 
static int ans(int n)
{
     
    // If the Number is less than 6 return
    // the number as it is.
    if (n < 6)
    {
        return n;
    }
     
    // Call the function again and again
    // the get the desired result.
    // And convert the number to base 6.
    return n % 6 + 10 * (ans(n / 6));
}
 
static int getSpecialNumber(int N)
{
     
    // Decrease the Number by 1 and Call
    // ans function to convert N to base 6
    return ans(--N);
}
 
/*
 * Example:- Input: N = 17 Output: 24
 *
 * Explaination:- decrease 17 by 1 N = 16 call ans() on 16
 *
 * ans(): 16%6 + 10*(ans(16/6)) since 16/6 = 2 it is less
 * than 6 the ans returns value as it is. 4 + 10*(2) = 24
 *
 * hence answer is 24.
 */
// Driver code
public static void main(String[] args)
{
    int N = 17;
    int answer = getSpecialNumber(N);
     
    System.out.println(answer);
}
}
 
// This code is contributed by Rajput-Ji

蟒蛇3

# Python3 code to find nth number
# with digits 0, 1, 2, 3, 4, 5
def ans(n):
 
    # If the Number is less than 6 return
    # the number as it is.
    if (n < 6):
        return n
     
    # Call the function again and again
    # the get the desired result.
    # And convert the number to base 6.
    return n % 6 + 10 * (ans(n // 6)) - 1
 
def getSpecialNumber(N):
     
    # Decrease the Number by 1 and Call
    # ans function to convert N to base 6
    return ans(N)
 
'''
 * Example:- Input: N = 17 Output: 24
 *
 * Explaination:- decrease 17 by 1 N = 16 call ans() on 16
 *
 * ans(): 16%6 + 10*(ans(16/6)) since 16/6 = 2 it is less than 6 the ans returns
 * value as it is. 4 + 10*(2) = 24
 *
 * hence answer is 24.
 '''
# Driver code
if __name__ == '__main__':
     
    N = 17
    answer = getSpecialNumber(N)
 
    print(answer)
 
# This code contributed by aashish1995

C#

// C# code to find nth number
// with digits 0, 1, 2, 3, 4, 5
using System;
 
public class GFG{
 
static int ans(int n)
{
     
    // If the Number is less than 6 return
    // the number as it is.
    if (n < 6)
    {
        return n;
    }
     
    // Call the function again and again
    // the get the desired result.
    // And convert the number to base 6.
    return n % 6 + 10 * (ans(n / 6));
}
 
static int getSpecialNumber(int N)
{
     
    // Decrease the Number by 1 and Call
    // ans function to convert N to base 6
    return ans(--N);
}
 
/*
 * Example:- Input: N = 17 Output: 24
 *
 * Explaination:- decrease 17 by 1 N = 16 call ans() on 16
 *
 * ans(): 16%6 + 10*(ans(16/6)) since 16/6 = 2 it is less
 * than 6 the ans returns value as it is. 4 + 10*(2) = 24
 *
 * hence answer is 24.
 */
// Driver code
public static void Main(String[] args)
{
    int N = 17;
    int answer = getSpecialNumber(N);
     
    Console.WriteLine(answer);
}
}
 
 
// This code is contributed by Rajput-Ji

Javascript


输出
24

时间复杂度: O(logN)
辅助空间: O(1)