gpt4 book ai didi

计算哈希集中的簇数

转载 作者:行者123 更新时间:2023-11-30 18:09:23 31 4
gpt4 key购买 nike

我正在尝试调试哈希集实现(用于学校作业)。冲突是通过线性探测来管理的,我需要计算调试例程的给定大小的簇的数量。我做了这个:

// really just hoping that a 50+ cluster doesn't occur
int clusters[50];
int count = 0;
for (int i=0; i < hashset->dim; i++) {
if (hashset->array[i] != NULL) {
count++;
} else {
if (count == 0) continue;
if (clusters[count] == NULL) clusters[count] = 0;
clusters[count]++;
count = 0;
}
}
for (int i=1; i < 50; i++) {
if (clusters[i] != NULL && clusters[i] != 0)
printf("%d clusters of size %d\n", clusters[i], i);
}

似乎有道理,但是当我运行它时,我得到......

25143 entries in hashset
50286 dimension of the hash array
4585 clusters of size 1
2134 clusters of size 2
1102 clusters of size 3
696 clusters of size 4
388 clusters of size 5
264 clusters of size 6
173 clusters of size 7
104 clusters of size 8
89 clusters of size 9
51 clusters of size 10
46 clusters of size 11
35 clusters of size 12
26 clusters of size 13
22 clusters of size 14
17 clusters of size 15
134553327 clusters of size 16
134634407 clusters of size 17
112 clusters of size 18
6 clusters of size 19
134553324 clusters of size 20
134634399 clusters of size 21
107 clusters of size 22
3 clusters of size 23
2 clusters of size 24
134634401 clusters of size 25
107 clusters of size 26
134107784 clusters of size 27
134556210 clusters of size 28
[... more nonsense]

所以最初它似乎给出了合理的输出..很多集群,但无论如何。我的想法是,太大的数字实际上应该是 0 - 它们是实际上并不存在的簇,但由于某种原因仍在打印。我只是不知道为什么......

最佳答案

这是一个简短的 C 程序:

#include <stdio.h>
int main() {
char buf[20];
for (int i = 0; i < 20; i++) {
printf("%d ", buf[i]);
}
printf("\n"); return 0;
}

仔细考虑运行时应该打印什么。

  • 每次运行时都会打印相同的内容吗?
  • 在每台运行它的机器上?

以下剧透:

(spoiler padding)













.

这是我在一次运行中看到的打印内容:0 0 0 0 0 0 0 0 -128 5 64 0 0 0 0 0 -16 -60 -55 31

同样的错误会影响我们的两个程序,但在我的程序中可能更容易看到。

关于计算哈希集中的簇数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2340271/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com