题解 | #位操作练习#
位操作练习
https://www.nowcoder.com/practice/7bdc346ca39841f6a05f73d98477621d
#include <stdio.h> #include <string.h> int a[16]; int b[16]; //输入的数如果不满16位,就要补0,但我输入的是按照类似于小端存储,因此将转换后的数求逆 void reverse(int a[], int length) { int b[N]; for (int i = 0; i < length; i ++) { b[i] = a[length - i - 1]; } memcpy(a, b, sizeof(b)); // for(int i = 0;i < 16;i ++){ // printf("%d ",a[i]); // } // printf("\n"); } void convert(int a[] ,int number) { int k = 0; while (number != 0) { int temp = number % 2; number /= 2; a[k ++] = temp; } reverse(a, 16); } int main() { int x, y; A:while (scanf("%d %d", &x, &y) != EOF) { // 注意 while 处理多个 case // 64 位输出请用 printf("%lld") to if(x == y){ puts("YES"); }else{ convert(a, x); convert(b, y); for(int j = 0;j < 16; j ++){ for(int i = 0; i < 16; i ++){ int k = 1; int m = a[0]; //左移 for(int i = 0; i <= 14;i ++){ a[i] = a[i + 1]; } a[15] = m; for(int i = 0; i < 16; i ++){ if(a[i] == b[i]){ //每位依次对比,16位都相同,则输出yes k ++; if(k == 16){ puts("YES"); goto A; } }else{ break; } } } } puts("NO"); } } return 0; }