📜  检查一个整数是否是另一个给定整数的旋转

📅  最后修改于: 2021-04-17 16:04:21             🧑  作者: Mango

给定两个整数AB ,任务是检查整数A是否为整数B的数字的旋转。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

方法:请按照以下步骤解决问题:

  • 如果A == B ,则打印“是”
  • 计算变量dig1dig2中存在于整数AB中的整数位数
  • 如果发现dig1!= dig2为真,则打印“ No”
  • 初始化一个变量,例如temp 。分配temp = A。
  • 现在,迭代并执行以下操作:
    • 如果A等于B并中断,则打印“是”
    • 检查(A == temp)是否A等于原始整数temp 。如果发现是真的,则打印“否”并中断。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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


输出:
Yes

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