Imprime todos los números saltantes menores o iguales a un valor dado

Un número se llama Número de salto si todos los dígitos adyacentes difieren en 1 . La diferencia entre ‘9 ‘ y ‘0’ no se considera como 1. 
Todos los números de un solo dígito se consideran números de salto. Por ejemplo, 7, 8987 y 4343456 son números de salto, pero 796 y 89098 no lo son. 
Dado un número positivo x , imprime todos los números saltadores menores o iguales que x . Los números se pueden imprimir en cualquier orden.

Ejemplo: 

C++14

#include <bits/stdc++.h>
 
using namespace std;
 
void print_sieve(int& x)
{
    int i,temp,digit;
    bool check;
     
    for(i=0;i<=x;i++)
    {
        if(i<10)
        {
            cout<<i<<" ";
            continue;
        }
        check=1;
        temp=i;
        digit=temp%10;
        temp/=10;
        while(temp)
        {
            if(abs(digit-temp%10)!=1)
            {
                check=0;
                break;
            }
            digit=temp%10;
            temp/=10;
        }
        if(check)
        cout<<i<<" ";
    }
}
 
int main()
{
    int x=105;
    print_sieve(x);
 
    return 0;
}

Java

// Java program to implement
// the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
  public static void print_sieve(int x)
  {
    int i, temp, digit;
    int check;
  
    for(i = 0; i <= x; i++)
    {
      if(i < 10)
      {
        System.out.print(i + " ");
        continue;
      }
      check = 1;
      temp = i;
      digit = temp % 10;
      temp /= 10;
      while(temp != 0)
      {
        if(Math.abs(digit - temp % 10) != 1)
        {
          check = 0;
          break;
        }
        digit = temp % 10;
        temp /= 10;
      }
      if(check != 0)
        System.out.print(i + " ");
    }
  }
  
  // Driver Code
  public static void main(String[] args)
  {
    int x = 105;
    print_sieve(x);
  }
}
 
// This code is contributed by Pushpesh Raj.

Python3

# Python3 program to implement the approach
 
# Function to print the jumping numbers
# in the range [0, x]
def print_sieve(x):
 
    # iterating over all the numbers
    # in the range [0, x]
    for i in range(x + 1):
        if(i < 10):
 
            # all numbers in [0, 9] are
            # jumping numbers
            print(i, end=" ")
            continue
 
        # the variable check tracks if
        # the number is valid
        check = 1
        temp = i
        digit = temp % 10
        temp //= 10
        while(temp > 0):
 
            if(abs(digit - temp % 10) != 1):
 
                check = 0
                break
 
            digit = temp % 10
            temp //= 10
 
        # printing i if check is 1
        if(check):
            print(i, end=" ")
 
# Driver Code
x = 105
print_sieve(x)
 
# This code is contributed by phasing17

C#

// C# program to implement
// the above approach
using System;
 
class GFG
{
 
  static void print_sieve(int x)
  {
    int i, temp, digit;
    int check;
 
    for(i = 0; i <= x; i++)
    {
      if(i < 10)
      {
        Console.Write(i + " ");
        continue;
      }
      check = 1;
      temp = i;
      digit = temp % 10;
      temp /= 10;
      while(temp != 0)
      {
        if(Math.Abs(digit - temp % 10) != 1)
        {
          check = 0;
          break;
        }
        digit = temp % 10;
        temp /= 10;
      }
      if(check != 0)
        Console.Write(i + " ");
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int x = 105;
    print_sieve(x);
  }
}
 
// This code is contributed by code_hunt.

Javascript

<script>
 
function print_sieve(x)
{
    let i,temp,digit;
    let check;
     
    for(i = 0; i <= x; i++)
    {
        if(i < 10)
        {
            document.write(i," ");
            continue;
        }
        check = 1;
        temp = i;
        digit = temp % 10;
        temp = Math.floor(temp / 10);
        while(temp)
        {
            if(Math.abs(digit - temp % 10) != 1)
            {
                check = 0;
                break;
            }
            digit = temp % 10;
            temp = Math.floor(temp / 10);
        }
        if(check)
            document.write(i," ");
    }
}
 
let x = 105;
print_sieve(x);
 
// This code is contributed by shinjanpatra
 
</script>

C++

// Finds and prints all jumping numbers smaller than or
// equal to x.
#include <bits/stdc++.h>
using namespace std;
 
// Prints all jumping numbers smaller than or equal to x starting
// with 'num'. It mainly does BFS starting from 'num'.
void bfs(int x, int num)
{
    // Create a queue and enqueue 'i' to it
    queue<int> q;
    q.push(num);
 
    // Do BFS starting from i
    while (!q.empty()) {
        num = q.front();
        q.pop();
 
        if (num <= x) {
            cout << num << " ";
            int last_dig = num % 10;
 
            // If last digit is 0, append next digit only
            if (last_dig == 0)
                q.push((num * 10) + (last_dig + 1));
 
            // If last digit is 9, append previous digit only
            else if (last_dig == 9)
                q.push((num * 10) + (last_dig - 1));
 
            // If last digit is neither 0 nor 9, append both
            // previous and next digits
            else {
                q.push((num * 10) + (last_dig - 1));
                q.push((num * 10) + (last_dig + 1));
            }
        }
    }
}
 
// Prints all jumping numbers smaller than or equal to
// a positive number x
void printJumping(int x)
{
    cout << 0 << " ";
    for (int i = 1; i <= 9 && i <= x; i++)
        bfs(x, i);
}
 
// Driver program
int main()
{
    int x = 40;
    printJumping(x);
    return 0;
}

Java

// Finds and prints all jumping numbers smaller than or
// equal to x.
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
 
    // Prints all jumping numbers smaller than or equal to x starting
    // with 'num'. It mainly does BFS starting from 'num'.
    public void bfs(int x, int num)
    {
        // Create a queue and enqueue 'i' to it
        Queue<Integer> q = new LinkedList<Integer>();
        q.add(num);
 
        // Do BFS starting from i
        while (!q.isEmpty()) {
            num = q.peek();
            q.poll();
            if (num <= x) {
                System.out.print(num + " ");
                int last_digit = num % 10;
 
                // If last digit is 0, append next digit only
                if (last_digit == 0) {
                    q.add((num * 10) + (last_digit + 1));
                }
 
                // If last digit is 9, append previous digit only
                else if (last_digit == 9) {
                    q.add((num * 10) + (last_digit - 1));
                }
 
                // If last digit is neither 0 nor 9, append both
                // previous and next digits
                else {
                    q.add((num * 10) + (last_digit - 1));
                    q.add((num * 10) + (last_digit + 1));
                }
            }
        }
    }
 
    // Prints all jumping numbers smaller than or equal to
    // a positive number x
    public void printJumping(int x)
    {
        System.out.print("0 ");
 
        for (int i = 1; i <= 9 && i <= x; i++) {
            this.bfs(x, i);
        }
    }
 
    // Driver program
    public static void main(String[] args) throws IOException
    {
        int x = 40;
        GFG obj = new GFG();
        obj.printJumping(x);
    }
}

Python3

# Class queue for use later
class Queue:
    def __init__(self):
        self.lst = []
 
    def is_empty(self):
        return self.lst == []
 
    def enqueue(self, elem):
        self.lst.append(elem)
 
    def dequeue(self):
        return self.lst.pop(0)
 
# Prints all jumping numbers smaller than or equal to
# x starting with 'num'. It mainly does BFS starting
# from 'num'.
def bfs(x, num):
 
    # Create a queue and enqueue i to it
    q = Queue()
    q.enqueue(num)
 
    # Do BFS starting from 1
    while (not q.is_empty()):
        num = q.dequeue()
 
        if num<= x:
            print(str(num), end =' ')
            last_dig = num % 10
 
            # If last digit is 0, append next digit only
            if last_dig == 0:
                q.enqueue((num * 10) + (last_dig + 1))
 
            # If last digit is 9, append previous digit
            # only
            elif last_dig == 9:
                q.enqueue((num * 10) + (last_dig - 1))
 
            # If last digit is neither 0 nor 9, append
            # both previous digit and next digit
            else:
                q.enqueue((num * 10) + (last_dig - 1))
                q.enqueue((num * 10) + (last_dig + 1))
 
# Prints all jumping numbers smaller than or equal to
# a positive number x
def printJumping(x):
    print (str(0), end =' ')
    for i in range(1, 10):
        bfs(x, i)
 
# Driver Program ( Change value of x as desired )
x = 40
printJumping(x)
 
# This code is contributed by Saket Modi

C#

// C# program to finds and prints all jumping
// numbers smaller than or equal to x.
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Prints all jumping numbers smaller than or
    // equal to x starting with 'num'. It mainly
    // does BFS starting from 'num'.
    public void bfs(int x, int num)
    {
        // Create a queue and enqueue 'i' to it
        Queue<int> q = new Queue<int>();
        q.Enqueue(num);
 
        // Do BFS starting from i
        while (q.Count!=0)
        {
            num = q.Peek();
            q.Dequeue();
            if (num <= x)
            {
                Console.Write(num + " ");
                int last_digit = num % 10;
 
                // If last digit is 0, append next digit only
                if (last_digit == 0)
                {
                    q.Enqueue((num * 10) + (last_digit + 1));
                }
 
                // If last digit is 9, append previous digit only
                else if (last_digit == 9)
                {
                    q.Enqueue((num * 10) + (last_digit - 1));
                }
 
                // If last digit is neither 0 nor 9, append both
                // previous and next digits
                else
                {
                    q.Enqueue((num * 10) + (last_digit - 1));
                    q.Enqueue((num * 10) + (last_digit + 1));
                }
            }
        }
    }
 
    // Prints all jumping numbers smaller than or equal to
    // a positive number x
    public void printJumping(int x)
    {
        Console.Write("0 ");
 
        for (int i = 1; i <= 9 && i <= x; i++)
        {
            this.bfs(x, i);
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int x = 40;
        GFG obj = new GFG();
        obj.printJumping(x);
    }
}
 
// This code has been contributed by 29AjayKumar

Javascript

<script>
 
// Finds and prints all jumping numbers
// smaller than or equal to x.
 
// Prints all jumping numbers smaller than
// or equal to x starting with 'num'. It
// mainly does BFS starting from 'num'.
function bfs(x, num)
{
     
    // Create a queue and enqueue 'i' to it
    let q = [];
    q.push(num);
 
    // Do BFS starting from i
    while (q.length != 0)
    {
        num = q.shift();
         
        if (num <= x)
        {
            document.write(num + " ");
            let last_digit = num % 10;
 
            // If last digit is 0, append next digit only
            if (last_digit == 0)
            {
                q.push((num * 10) + (last_digit + 1));
            }
 
            // If last digit is 9, append previous
            // digit only
            else if (last_digit == 9)
            {
                q.push((num * 10) + (last_digit - 1));
            }
 
            // If last digit is neither 0 nor 9,
            // append both previous and next digits
            else
            {
                q.push((num * 10) + (last_digit - 1));
                q.push((num * 10) + (last_digit + 1));
            }
        }
    }
}
 
// Prints all jumping numbers smaller
// than or equal to a positive number x
function printJumping(x)
{
    document.write("0 ");
   
    for(let i = 1; i <= 9 && i <= x; i++)
    {
        bfs(x, i);
    }
}
 
// Driver code
let x = 40;
printJumping(x);
 
// This code is contributed by rag2127
 
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 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 *