题目地址:http://ac.jobdu.com/problem.php?pid=1151

题目1151:位操作练习

时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1687 解决:927

题目描述:

给出两个不大于65535的非负整数,判断其中一个的16位二进制表示形式,是否能由另一个的16位二进制表示形式经过循环左移若干位而得到。

循环左移和普通左移的区别在于:最左边的那一位经过循环左移一位后就会被移到最右边去。比如:
1011 0000 0000 0001 经过循环左移一位后,变成 0110 0000 0000 0011, 若是循环左移2位,则变成 1100 0000 0000 0110

输入:

第一行是个整数n, 0 < n < 300000,表示后面还有n行数据
后面是n行,每行有两个不大于65535的非负整数

输出:

对于每一行的两个整数,输出一行,内容为YES或NO

样例输入:
4
2 4
9 18
45057 49158
7 12
样例输出:
YES
YES
YES
NO
来源:
2010年北京大学计算机研究生机试真题
答疑:
解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7874-1-1.html
代码:
#include <cstdio>
bool cmp(unsigned short a, unsigned short b){
    const int N = sizeof(unsigned short) * 8;
    for(int i = 0; i < N; ++i){
        unsigned short t = (b << i) | (b >> (N - i));
        if(t == a){
            return true;
        }
    }
    return false;
}

int main() {
    int n = 0;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            unsigned short a,b;
            scanf("%hu%hu", &a, &b);
            if(cmp(a, b)){
                printf("YES\n");
            } else {
                printf("NO\n");
            }
        }
    }
    return 0;
}