-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashing - Average Search Time.cpp
65 lines (62 loc) · 1.24 KB
/
Hashing - Average Search Time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
int hash[10010] = {0};
bool isprime(int v)
{
if (v == 1)
return false;
for (int i = 2; i * i <= v; i++)
{
if (v % i == 0)
return false;
}
return true;
}
int main()
{
int m, n, k;
std::cin >> m >> n >> k;
while (!isprime(m))
{
m++;
}
for (int i = 0; i < n; i++)
{
int temp;
std::cin >> temp;
int position;
int flag = 0;
for (int j = 0; j < m; j++)
{
position = (temp + j * j) % m;
if (hash[position] == 0)
{
hash[position] = temp;
flag = 1;
break;
}
}
if (flag == 0)
std::cout << temp << " cannot be inserted." << std::endl;
}
double sum = 0;
double num = 0;
for (int i = 0; i < k; i++)
{
int temp;
std::cin >> temp;
int position;
int j;
for (j = 0; j < m; j++)
{
position = (temp + j * j) % m;
if (hash[position] == temp || hash[position] == 0)
{
break;
}
}
sum += j + 1;
num++;
}
printf("%0.1f", sum / num);
return 0;
}