Title Address: http://ac.jobdu.com/problem.php?pid=1151
Topic 1151: Bit manipulation exercises
time limit:1 seconds memory limit:32 trillion special sentence: no submission:1687 resolution:927
-
Title Description:
-
Give two nonnegative integers not greater than 65535, and determine whether one of the 16-bit binary representations can be obtained by another 16-bit binary representation by looping left several bits.
The difference between a circular left shift and a normal left shift is that the leftmost bit will be moved to the far right after the left one is cycled. Like what:
1011 0000 0000 0001 After a loop left one, it becomes 0110 0000 0000 0011, if the loop moves left 2 bits, it becomes 1100 0000 0000 0110
-
Input:
-
The first line is an integer n, 0 < n < 300000, indicating that there are still n rows of data behind
followed by n rows, with two non-negative integers not greater than 65535 per line
-
Output:
-
For two integers per line, output a line with Yes or no
-
Sample input:
-
42 49 1845057) 491587 12
-
Sample output:
-
Yesyesyesno
-
Source:
- 2010 Peking University computer Research Life Test real problem
-
Answering:
-
Problem solving problems? to discuss topics please visit: http://t.jobdu.com/thread-7874-1-1.html
-
code:
-
#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;}
Topic 1151: Bit manipulation exercises