There are 1 to 10 W of the 10 W number, remove 2 and disrupt the order, how to find the two numbers
Description: there are 1 to 10 W numbers, remove 2 and disrupt the order, and how to find the two numbers.
1. Bitmap method:
Assume that the array to be processed is a [10w-2]
Define an array B [10 W]. Here we assume that each element in B occupies 1 bit and is initialized to all 0
For (I = 0; I <10w-2; I ++)
{
B [A [I] = 1
}
Then, the zero element in B is the missing data.
This method is very efficient and is one of the most commonly used algorithms in computers.
2. Calculate the sum of sum and sum of squares between 1 and 10 W.
Then calculate the sum and sum of squares of the given number.
The sum and sum of squares of the two numbers are obtained.
So we have
X + y = N
X ^ 2 + y ^ 2 = m
The values of X and Y can be obtained by solving the equation.
Sum and sum of squares can get the result, but it is possible that the sum of squares has a large amount of computing (64-bit int will not overflow)
Const int size = 1000;
Int main (INT argc, char * argv [])
{
Int A [] = {2, 3, 4, 5, 6, 7 };
Int B [size] = {0 };
For (INT I = 0; I <sizeof (a)/sizeof (A [0]); I ++ ){
B [A [I] = 1;
}
For (Int J = 0; j <size; j ++ ){
If (B [J] = 0 ){
Printf ("% d \ t", J );
}
}
Return 0;
}