📌  相关文章
📜  第N个数字仅由奇数位组成

📅  最后修改于: 2021-04-21 21:46:00             🧑  作者: Mango

给定整数N ,任务是查找仅由奇数位(1、3、5、7、9)组成的第N个数字。
由奇数数字组成的前几个数字是1、3、5、7、9、11、13、15、17、19、31,…

例子:

方法1(简单):1开始,继续检查数字是否仅由奇数数字( 1、3、5、7、9 )组成,并在找到第n个数字时停止。

下面是上述方法的实现:

C++
// C++ program to find nth number made up of odd digits only
#include 
using namespace std;
 
// Function to return nth number made up of odd digits only
int findNthOddDigitNumber(int n)
{
 
    // Variable to keep track of how many
    // such elements have been found
    int count = 0;
    for (int i = 1;; i++) {
        int num = i;
        bool isMadeOfOdd = true;
 
        // Checking each digit of the number
        while (num != 0) {
 
            // If 0, 2, 4, 6 or 8 is found
            // then the number is not made up of odd digits
            if (num % 10 == 0
                || num % 10 == 2
                || num % 10 == 4
                || num % 10 == 6
                || num % 10 == 8) {
                isMadeOfOdd = false;
                break;
            }
 
            num = num / 10;
        }
 
        // If the number is made up of odd digits only
        if (isMadeOfOdd == true)
            count++;
 
        // If it is the nth number
        if (count == n)
            return i;
    }
}
 
// Driver Code
int main()
{
    int n = 10;
    cout << findNthOddDigitNumber(n);
    return 0;
}


Java
// Java program to find nth number
// made up of odd digits only
 
import java.io.*;
 
class GFG {
    // Function to return nth number made up of odd digits only
static int findNthOddDigitNumber(int n)
{
 
    // Variable to keep track of how many
    // such elements have been found
    int count = 0;
    for (int i = 1;; i++) {
        int num = i;
        boolean isMadeOfOdd = true;
 
        // Checking each digit of the number
        while (num != 0) {
 
            // If 0, 2, 4, 6 or 8 is found
            // then the number is not made up of odd digits
            if (num % 10 == 0
                || num % 10 == 2
                || num % 10 == 4
                || num % 10 == 6
                || num % 10 == 8) {
                isMadeOfOdd = false;
                break;
            }
 
            num = num / 10;
        }
 
        // If the number is made up of odd digits only
        if (isMadeOfOdd == true)
            count++;
 
        // If it is the nth number
        if (count == n)
            return i;
    }
}
 
// Driver Code
     
    public static void main (String[] args) {
    int n = 10;
    System.out.println (findNthOddDigitNumber(n));
         
    }
//This code is contributed by ajit   
}


Python3
# Python3 program to find nth number
# made up of odd digits only
 
# Function to return nth number made
# up of odd digits only
def findNthOddDigitNumber(n) :
     
    # Variable to keep track of how many
    # such elements have been found
    count = 0
     
    i = 1
    while True :
        num = i
        isMadeOfOdd = True
         
        # Checking each digit of the number
        while num != 0 :
             
            # If 0, 2, 4, 6 or 8 is found
            # then the number is not made
            # up of odd digits
            if (num % 10 == 0 or num % 10 == 2 or
                num % 10 == 4 or num % 10 == 6 or
                num % 10 == 8) :
                     
                    isMadeOfOdd = False
                    break
         
            num /= 10
     
        # If the number is made up of
        # odd digits only
        if isMadeOfOdd == True :
            count += 1
     
        # If it is the nth number
        if count == n :
            return i
     
        i += 1
 
# Driver code
if __name__ == "__main__" :
     
    n = 10
     
    # Function call
    print(findNthOddDigitNumber(n))
     
# This code is contributed by Ryuga


C#
// C# program to find nth number
// made up of odd digits only
using System;
 
class GFG
{
     
// Function to return nth number
// made up of odd digits only
static int findNthOddDigitNumber(int n)
{
 
    // Variable to keep track of
    // how many such elements have
    // been found
    int count = 0;
    for (int i = 1;; i++)
    {
        int num = i;
        bool isMadeOfOdd = true;
 
        // Checking each digit
        // of the number
        while (num != 0)
        {
 
            // If 0, 2, 4, 6 or 8 is found
            // then the number is not made
            // up of odd digits
            if (num % 10 == 0 || num % 10 == 2 ||
                num % 10 == 4 || num % 10 == 6 ||
                num % 10 == 8)
            {
                isMadeOfOdd = false;
                break;
            }
 
            num = num / 10;
        }
 
        // If the number is made up of
        // odd digits only
        if (isMadeOfOdd == true)
            count++;
 
        // If it is the nth number
        if (count == n)
            return i;
    }
}
 
// Driver Code
static public void Main ()
{
    int n = 10;
    Console.WriteLine(findNthOddDigitNumber(n));
}
}
 
// This code is contributed
// by Ajit Deshpal


PHP


Javascript


输出:
19

方法2(基于队列):想法是生成仅包含奇数位的所有数字(小于n)。如何生成所有小于n且具有奇数位的数字?我们为此使用队列。最初,我们将“ 1”,“ 3”,“ 5”,“ 7”和“ 9”推入队列。然后,当已处理元素的数量小于n时,我们将运行一个循环。我们一个接一个地弹出一个项目,对于每个弹出的项目x,我们生成下一个数字x * 10 + 1,x * 10 + 3,x * 10 + 5,x * 10 + 7和x * 10 + 9。这些新数字。该方法的时间复杂度为O(n)
请参阅以下帖子以了解此方法的实现。
小于N的二进制数字计数