There is an integer array that requests the minimum absolute value of the difference between the two. Remember, you only need to obtain the minimum value and do not need to find the two numbers. Below is a knot, and STL sort is used to sort the data late.
# Include <iostream>
# Include <vector>
# Include <ctime>
# Include <cstdio>
# Include <cmath>
Using namespace STD;
# Define n 100
# Define m 200
Int inline DIS (int A, int B)
{
Return ABS (a-B );
}
Void print (Int &)
{
Cout <"<A <"";
}
Int main ()
{
Int I, Min, TMP;
Vector <int> VI;
Srand (Time (null ));
For (I = 0; I <n; I ++)
Vi. push_back (TMP = rand () % m, TMP % 2 = 0? -TMP: TMP ));
Sort (VI. Begin (), VI. End (), less <int> ());
// For_each (VI. Begin (), VI. End (), print );
For (I = 1; I <VI. Size (); I ++ )...{
TMP = DIS (VI [I-1], VI [I]);
If (I = 1)... {min = TMP; continue;} If (! TMP)... {min = 0; break ;}
If (TMP <min) min = TMP;
}
Cout <Endl <"minimal distance is:" <min <Endl;
Return 0;
}
We have seen such a solution to find the maximum and minimum values in the array and map all the numbers to the Bit Array in this range. This is the minimum difference between two adjacent bits of 1. If there is the same value, it will be mapped to a bit, then the minimum absolute value is 0.