给出两个不大于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
C++代码:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
unsigned short x,y;
cin>>x>>y;
int m=16;
bool flag=false;
while(m--)
{
if(x==y)
{
cout<<"YES"<<endl;
flag=true;
break;
}//if
if(x>=(1<<15))
{
x=(x<<1)+1;
}
else
x<<=1;
}//while
if(!flag)
cout<<"NO"<<endl;
}//for
return 1;
}//
/**************************************************************
Problem: 1151
User: Carvin
Language: C++
Result: Accepted
Time:0 ms
Memory:1520 kb
****************************************************************/package oj1151;
import java.util.Scanner;
public class oj1151{
private Scanner in;
public oj1151() {
// TODO Auto-generated constructor stub
in=new Scanner(System.in);
int n=in.nextInt();
for(int i=0;i<n;i++){
int a=in.nextInt();
int b=in.nextInt();
String str1=Integer.toBinaryString(a);
String str2=Integer.toBinaryString(b);
//System.out.println(str1);
//System.out.println(str2);
for(int j=0;j<16-str1.length();j++)
str1="0"+str1;
for(int j=0;j<16-str2.length();j++)
str2="0"+str2;
str1+=str1;
if(str1.contains(str2))
System.out.println("YES");
else
System.out.println("NO");
}
}
public static void main(String args[]){
oj1151 x=new oj1151();
}
}//oj1151版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/carvin_zh/article/details/46703273