http://hxraid.iteye.com/blog/618153
Q: After the number of 100 consecutive numbers is disrupted, we randomly retrieve one number. How can we quickly determine which one is missing?
Analysis: For all 100 consecutive numbers, we only need to divide the remaining 100. It must be between 0 and ~ Between 99. In general, the conventional approach is to first sort (locate using the hash table) and search in a loop. Of course, the time complexity is O (2n ). Now we will introduce a very good O (n) method: Calculate binary XOR.
Exclusive or operation: 0 ^ 0 = 1 ^ 1 = 0; 0 ^ 1 = 1 ^ 0 = 1. 0 ~ The result of 99 numbers all exclusive or can only be 0. If a number is missing, the result of all XOR is exactly that number. Why? Let's make a small experiment: if there are four numbers: 0001 0110, and, they are arranged into a matrix.
Bits: 1 2 3 4
0 0 0 1
0 0 1 0
0 1 0 1
0 1 1 0
All XOR: 0 0 0 0
We can draw conclusions,If the result of all XOR operations is 0, the number of 1 in all bits must be an even number.Conversely, if one of the numbers does not exist (for example, 0001), the values on the bit with less than 0 remain unchanged (because the number of 1 is still an even number ), the value of 1 bit becomes 1 (the number of 1 is an odd number ).
Here I think of several examples of using an exclusive or to perform operations:
First: use an exclusive or exchange value of two numbers
Second: use an exclusive or to select a large or small number.
Third: a sequence contains an odd number, and only one of the numbers is displayed in pairs, so you can quickly find the number that appears separately.
0 ~ The principle of 99 is the same, so the difference or result is the smaller value. The Code is as follows:
int data=0;for (int i=0;i<=99;i++){if(70==i)continue;data=data^i;}cout << data << endl;
2. Question: After 100 consecutive numbers are disrupted, two numbers are randomly taken out. How can we quickly determine which two are missing? (Note that the number is less than 2)
Analysis: a common practice is to create a hash table with 100 structures, and then set the position after all hash values of 100 to 1 at a time. Then, the hash table is searched for 100 times sequentially. O (2n) time is required before and after. But is there any faster way? Of course, directly operate bit.
Suppose we have 32 consecutive numbers (0 ~ 31) two numbers 2 and 11 are missing, and you want to mark 1 on a 32-bit. That is, an integer variable. After marking it, it becomes:
Bits Position 31 30 29 28... 11 10... 2 1 0
Int A = 1 1 1 1... 0 1 (the bit of the missing number is 0)
As for how to mark it as a, let's look at the following code:
# Include <iostream> # include <stack> using namespace STD; typedef stack <int> Sint; Sint tobinary (unsigned int N) {sint s; int I = n/2; // record business value Int J = n % 2; // record remainder while (1) {S. push (j); If (0 = I) break; j = I % 2; I = I/2;} return s;} int main () {unsigned int bits = 0; For (INT I = 0; I <32; I ++) {unsigned int bitmove = 1; if (I = 2 | I = 11) continue; bitmove = bitmove <I; bits = bits | bitmove;} Sint stmp; stmp = tobinary (BITs ); while (! Stmp. Empty () {cout <stmp. Top (); stmp. Pop ();} return 0 ;}