Comprobar si un entero es la rotación de otro entero dado

Dados dos enteros A y B , la tarea es verificar si el entero A es una rotación de los dígitos del entero B o no. Si se encuentra que es cierto, escriba «Sí» . De lo contrario, escriba “No” .

Ejemplos: 
 

Entrada: A= 976, B= 679
Salida:

Entrada: A= 974, B= 2345
Salida: No

Enfoque: siga los pasos a continuación para resolver el problema:

  • Si A == B , imprima «Sí» .
  • Calcule el recuento de dígitos del entero presente en el entero A y B en las variables, digamos dig1 y dig2 .
  • Si se encuentra que dig1 != dig2 no es verdadero, entonces imprima “No” .
  • Inicialice una variable, digamos temp . Asigne temp = A .
  • Ahora, itera y realiza las siguientes operaciones:
    • Imprime “Sí” si A es igual a B y rompe.
    • Compruebe si (A== temp), es decir , A se vuelve igual a la temperatura entera original . Si se encuentra que es cierto, escriba «No» y rompa.

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

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the integer
// A is a rotation of the integer B
int check(int A, int B)
{
 
    if (A == B) {
        return 1;
    }
 
    // Stores the count of digits in A
    int dig1 = floor(log10(A) + 1);
 
    // Stores the count of digits in B
    int dig2 = floor(log10(B) + 1);
 
    // If dig1 not equal to dig2
    if (dig1 != dig2) {
        return 0;
    }
 
    int temp = A;
 
    while (1) {
 
        // Stores position of first digit
        int power = pow(10, dig1 - 1);
 
        // Stores the first digit
        int firstdigit = A / power;
 
        // Rotate the digits of the integer
        A = A - firstdigit * power;
        A = A * 10 + firstdigit;
 
        // If A is equal to B
        if (A == B) {
            return 1;
        }
        // If A is equal to the initial
// value of integer A
        if (A == temp) {
            return 0;
        }
    }
}
 
// Driver Code
int main()
{
    int A = 967, B = 679;
 
    if (check(A, B))
        cout << "Yes";
    else
        cout << "No" << endl;
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
class GFG {
 
    // Function to check if the integer
    // A is a rotation of the integer B
    static int check(int A, int B)
    {
 
        if (A == B) {
            return 1;
        }
 
        // Stores the count of digits in A
        int dig1 = (int)Math.floor(Math.log10(A) + 1);
 
        // Stores the count of digits in B
        int dig2 = (int)Math.floor(Math.log10(B) + 1);
 
        // If dig1 not equal to dig2
        if (dig1 != dig2) {
            return 0;
        }
 
        int temp = A;
 
        while (true) {
 
            // Stores position of first digit
            int power = (int)Math.pow(10, dig1 - 1);
 
            // Stores the first digit
            int firstdigit = A / power;
 
            // Rotate the digits of the integer
            A = A - firstdigit * power;
            A = A * 10 + firstdigit;
 
            // If A is equal to B
            if (A == B) {
                return 1;
            }
            // If A is equal to the initial
            // value of integer A
            if (A == temp) {
                return 0;
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A = 967, B = 679;
 
        if (check(A, B) == 1)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Dharanendra L V.

Python3

# Python3 implementation of the approach
import math
 
# Function to check if the integer
# A is a rotation of the integer B
def check(A, B) :
  
    if (A == B) :
        return 1
  
    # Stores the count of digits in A
    dig1 = math.floor(math.log10(A) + 1)
  
    # Stores the count of digits in B
    dig2 = math.floor(math.log10(B) + 1)
  
    # If dig1 not equal to dig2
    if (dig1 != dig2) :
        return 0
  
    temp = A
  
    while (True) :
  
        # Stores position of first digit
        power = pow(10, dig1 - 1)
  
        # Stores the first digit
        firstdigit = A // power
  
        # Rotate the digits of the integer
        A = A - firstdigit * power
        A = A * 10 + firstdigit
  
        # If A is equal to B
        if (A == B) :
            return 1
         
        # If A is equal to the initial value of integer A
        if (A == temp) :
            return 0
           
          # Driver code
A, B = 967, 679
  
if (check(A, B)) :
    print("Yes")
else :
    print("No")
     
    # This code is contributed by divyesh072019.

C#

// C# implementation of the approach
using System;
public class GFG
{
 
    // Function to check if the integer
    // A is a rotation of the integer B
    static int check(int A, int B)
    {
        if (A == B)
        {
            return 1;
        }
 
        // Stores the count of digits in A
        int dig1 = (int)Math.Floor(Math.Log10(A) + 1);
 
        // Stores the count of digits in B
        int dig2 = (int)Math.Floor(Math.Log10(B) + 1);
 
        // If dig1 not equal to dig2
        if (dig1 != dig2)
        {
            return 0;
        }
        int temp = A;
        while (true)
        {
 
            // Stores position of first digit
            int power = (int)Math.Pow(10, dig1 - 1);
 
            // Stores the first digit
            int firstdigit = A / power;
 
            // Rotate the digits of the integer
            A = A - firstdigit * power;
            A = A * 10 + firstdigit;
 
            // If A is equal to B
            if (A == B)
            {
                return 1;
            }
           
            // If A is equal to the initial
            // value of integer A
            if (A == temp)
            {
                return 0;
            }
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int A = 967, B = 679;
        if (check(A, B) == 1)
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
    // Javascript implementation of the approach
     
    // Function to check if the integer
    // A is a rotation of the integer B
    function check(A, B)
    {
        if (A == B)
        {
            return 1;
        }
  
        // Stores the count of digits in A
        let dig1 = Math.floor(Math.log10(A) + 1);
  
        // Stores the count of digits in B
        let dig2 = Math.floor(Math.log10(B) + 1);
  
        // If dig1 not equal to dig2
        if (dig1 != dig2)
        {
            return 0;
        }
        let temp = A;
        while (true)
        {
  
            // Stores position of first digit
            let power = Math.pow(10, dig1 - 1);
  
            // Stores the first digit
            let firstdigit = parseInt(A / power, 10);
  
            // Rotate the digits of the integer
            A = A - firstdigit * power;
            A = A * 10 + firstdigit;
  
            // If A is equal to B
            if (A == B)
            {
                return 1;
            }
            
            // If A is equal to the initial
            // value of integer A
            if (A == temp)
            {
                return 0;
            }
        }
    }
     
    let A = 967, B = 679;
    if (check(A, B) == 1)
      document.write("Yes");
    else
      document.write("No");
   
  // This code is contributed by suresh07.
</script>
Producción: 

Yes

 

Complejidad de tiempo: O(dígito(N))
Espacio auxiliar: O(1) 

Publicación traducida automáticamente

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