给定两个正整数x和y,检查一个整数是否通过旋转另一个整数的位获得。
null
Input constraint: 0 < x, y < 2^32
钻头旋转: 旋转(或循环移位)是一种类似于移位的操作,只是一端脱落的位被放回另一端。 有关钻头旋转的更多信息,请参见 在这里 例1:
Input : a = 8, b = 1Output : yesExplanation :Representation of a = 8 : 0000 0000 0000 0000 0000 0000 0000 1000Representation of b = 1 : 0000 0000 0000 0000 0000 0000 0000 0001If we rotate a by 3 units right we get b, hence answer is yes
例2:
Input : a = 122, b = 2147483678Output : yesExplanation :Representation of a = 122 : 0000 0000 0000 0000 0000 0000 0111 1010Representation of b = 2147483678 : 1000 0000 0000 0000 0000 0000 0001 1110If we rotate a by 2 units right we get b, hence answer is yes
因为x或y可以表示的总位是32位,因为x,y>0,x,y<2^32。 所以我们需要找到x的所有32个可能的旋转,并将其与y进行比较,直到x和y不相等。 为此,我们使用一个64位的临时变量x64,它是x到x的级联结果。。 x64的前32位与x的位相同,后32位也与x64的位相同。 然后我们继续在右边将x64移位1,并将x64最右边的32位与y进行比较。 这样我们就可以得到由于旋转而产生的所有可能的位组合。 下面是上述算法的实现。
C++
// C++ program to check if two numbers are bit rotations // of each other. #include <iostream> using namespace std; // function to check if two numbers are equal // after bit rotation bool isRotation(unsigned int x, unsigned int y) { // x64 has concatenation of x with itself. unsigned long long int x64 = x | ((unsigned long long int )x << 32); while (x64 >= y) { // comparing only last 32 bits if (unsigned(x64) == y) return true ; // right shift by 1 unit x64 >>= 1; } return false ; } // driver code to test above function int main() { unsigned int x = 122; unsigned int y = 2147483678; if (isRotation(x, y)) cout << "yes" << endl; else cout << "no" << endl; return 0; } |
JAVA
// Java program to check if two numbers are bit rotations // of each other. class GFG { // function to check if two numbers are equal // after bit rotation static boolean isRotation( long x, long y) { // x64 has concatenation of x with itself. long x64 = x | (x << 32 ); while (x64 >= y) { // comparing only last 32 bits if (x64 == y) { return true ; } // right shift by 1 unit x64 >>= 1 ; } return false ; } // driver code to test above function public static void main(String[] args) { long x = 122 ; long y = 2147483678L; if (isRotation(x, y) == false ) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program to check if two # numbers are bit rotations of each other. # function to check if two numbers # are equal after bit rotation def isRotation(x, y) : # x64 has concatenation of x # with itself. x64 = x | (x << 32 ) while (x64 > = y) : # comparing only last 32 bits if ((x64) = = y) : return True # right shift by 1 unit x64 >> = 1 return False # Driver Code if __name__ = = "__main__" : x = 122 y = 2147483678 if (isRotation(x, y) = = False ) : print ( "yes" ) else : print ( "no" ) # This code is contributed by Ryuga |
C#
// C# program to check if two numbers // are bit rotations of each other. using System; class GFG { // function to check if two numbers // are equal after bit rotation static bool isRotation( long x, long y) { // x64 has concatenation of // x with itself. long x64 = x | (x << 32); while (x64 >= y) { // comparing only last 32 bits if (x64 == y) { return true ; } // right shift by 1 unit x64 >>= 1; } return false ; } // Driver Code public static void Main() { long x = 122; long y = 2147483678L; if (isRotation(x, y) == false ) { Console.Write( "Yes" ); } else { Console.Write( "No" ); } } } // This code is contributed // by 29AjayKumar |
PHP
<?php // PHP program to check if two // numbers are bit rotations of // each other. // function to check if two // numbers are equal after // bit rotation function isRotation( $x , $y ) { // x64 has concatenation // of x with itself. $x64 = $x | ( $x << 32); while ( $x64 >= $y ) { // comparing only last 32 bits if (( $x64 ) == $y ) return 1; // right shift by 1 unit $x64 >>= 1; } return -1; } // Driver Code $x = 122; $y = 2147483678; if (isRotation( $x , $y )) echo "yes" , "" ; else echo "no" , "" ; // This code is contributed by aj_36 ?> |
Javascript
<script> // javascript program to check if two numbers are bit rotations // of each other. // function to check if two numbers are equal // after bit rotation function isRotation(x, y) { // x64 has concatenation of x with itself. var x64 = x | (x << 32); while (x64 >= y) { // comparing only last 32 bits if (x64 == y) { return true ; } // right shift by 1 unit x64 >>= 1; } return false ; } // driver code to test above function var x = 122; var y = 2147483678; if (isRotation(x, y) == false ) { document.write( "Yes" ); } else { document.write( "No" ); } // This code is contributed by 29AjayKumar </script> |
输出:
yes
本文由 普拉提克·切哈杰 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END