A, 7-1 hash (30 minutes)
the task of this problem is simple: the number of different positive integers inserted into the hash table, and outputs the input digital position.
Is defined as the hash function H (Key) Key =% TSize, TSize hash table where the maximum size.
Secondary detection (only with a positive increment) to resolve the conflict.
Note that the table size is best to prime.
If the maximum size is not a prime number given by the user, the table must be redefined size of the largest prime number, the prime number is greater than the size given by the user.
Input Specifications:
Each input file contains a test case.
In each case, the first line contains two positive: MSize (≤104) and N (≤MSize), which are input table size and number of user-defined.
Then the next line gives N different positive integers.
All numbers in a row are separated by spaces.
Output Specification:
For each test, prints the input line in the corresponding position number (0 indexed).
All numbers in a row are separated by spaces, and the end of the line can not have extra spaces.
If you can not insert number, print "-."
Sample input:
. 4. 4
10. 4 15. 6
Sample Output:
014--
1, determines a number is not a prime number
BOOL if_prime ( int a) { int I; // custom parameter IF (a == 1 || a == 0 ) return to false ; // If a is 0 or 1, is not prime return flase for (I = 2 ; I <= sqrt (A); I ++) // sqrt () the square root function (?? // remember added #include <math.h> { IF (A% I == 0 ) return to false ; // A is not a prime number, Back flase } return to true ; // A is a prime number, returns to true }
2, to obtain a minimum equal to a prime number greater than
int get_prime ( int a) { the while (if_prime (a)!) // if not a prime number { a ++; // a value + 1'd } return a; // return a }
3, the main function
int main () { int m, n-; // custom parameter CIN >> >> n-m; // input table length and the number of inputs m = get_prime (m); // table length is defined as the smallest prime number greater than or equal to m int visited [ 10007 ] = { 0 }; // define an array access and the all 0 int iNPUT [n-]; // define the input array int I, J, K; // define the parameters for (I = 0 ; I <n- ; I ++ ) { CIN >> iNPUT [I]; // input sequence of positive integers } for (I = 0 ; I <n-; I ++ ) { J = INPUT [I]% m; // get the value should be inserted IF (visited [J] == 0 ) // if this position is not inserted { COUT << J; // output the position value IF ( I <N- . 1 ) COUT << " " ; // determines whether the output space visited [J] = . 1 ; // indicates that the location has been inserted } the else // if the position has been inserted { for (K = . 1 ; K <m; K ++ ) { J= (K * K + INPUT [I])% m; // use quadratic probe method conflict IF (visited [J] == 0 ) // if the processing is not inserted position { COUT << J; // output position values iF (I <N- . 1 ) COUT << " " ; // determines whether the output space visited [J] = . 1 ; // indicates that the location has been inserted BREAK ; // out of the loop } } iF ( m == K) // if k = m, it can not be inserted { COUT<< " - " ; // Output '-' IF (I <N- . 1 ) COUT << " " ; // determine whether to output spaces } } } return 0 ; }
Second, the goal:
1) Review of previous sections Science
2) to seriously study the next chapter and timely review and complete the job, do the advance prep