Número enésimo formado solo por dígitos impares

Dado un número entero N , la tarea es encontrar el número N formado solo por dígitos impares (1, 3, 5, 7, 9). 
Los primeros números formados por dígitos impares son 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 31, …

Ejemplos:  

Entrada: N = 7 
Salida: 13 
1, 3, 5, 7, 9, 11, 13 
13 es el séptimo número de la serie

Entrada: N = 10 
Salida: 19 

Método 1 (simple): a partir de 1 , siga comprobando si el número está formado únicamente por dígitos impares (1, 3, 5, 7, 9) y deténgase cuando se encuentre el enésimo número.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ program to find nth number made up of odd digits only
#include <bits/stdc++.h>
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

<?php
// PHP program to find nth number
// made up of odd digits only
 
// Function to return nth number
// made up of odd digits only
function 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 = (int)($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
$n = 10;
 
// Function call
print(findNthOddDigitNumber($n));
     
// This code is contributed by mits
?>

Javascript

<script>
 
// JavaScript program to find nth
// number made up of odd digits only
 
// Function to return nth number
// made up of odd digits only
function findNthOddDigitNumber(n)
{
     
    // Variable to keep track of how many
    // such elements have been found
    let count = 0;
    for(let i = 1;; i++)
    {
        let num = i;
        let 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 = Math.floor(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
let n = 10;
 
document.write(findNthOddDigitNumber(n));
 
// This code is contributed by Manoj.
 
</script>
Producción: 

19

 

Complejidad de tiempo: O(n log 10 n), donde N representa el tamaño entero.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
 

Enfoque 2 (basado en cola): la idea es generar todos los números (menores que n) que contengan solo dígitos impares. ¿Cómo generar todos los números menores que n con dígitos impares? Usamos la cola para esto. Inicialmente empujamos ‘1’, ‘3’, ‘5’, ‘7’ y ‘9’ a la cola. Luego ejecutamos un bucle mientras el recuento de elementos procesados ​​es menor que n. Abrimos un elemento uno por uno y para cada elemento x, generamos los siguientes números x*10 + 1, x*10 + 3, x*10 + 5, x*10 + 7 y x*10 + 9. Ponemos en cola estos nuevos números. La complejidad de tiempo de este enfoque es O(n).
Consulte la publicación a continuación para ver la implementación de este enfoque. 
Recuento de números de dígitos binarios menores que N
 

Publicación traducida automáticamente

Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *