#include <stdio.h> #include <string.h> #define Tablesize 1000005 int zzt[Tablesize],count; void insert(char *ID, int distance); struct Record { char ID[19]; int distance; int he; }Record; struct Record records[Tablesize]; int hash(char *ID); int find(char *ID); int main() { char ID[19]; for (int i = 0; i < sizeof(zzt) / sizeof(zzt[0]); i++) { zzt[i] = -1;} int n, k,m,distance; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%s %d", ID, &distance); if (distance < k) distance = k; insert(ID, distance); } scanf("%d", &m); int t = -1; for (int i = 0; i < m; i++) { scanf("%s", ID); t = find(ID); if (t == -1) printf("No Info\n"); else printf("%d\n", records[t].distance); } } int find(char *ID) { int h = hash(ID); for (int i = zzt[h]; i != -1; i = records[i].he) if (!strcmp(records[i].ID, ID)) return i; return -1; } int hash(char *ID) { long int yy = 0; for (int i = 0; i < 17; i++) yy = 10 * yy + (ID[i] - '0'); return yy%Tablesize; } void insert(char *ID, int distance) { int o = hash(ID); for (int i = zzt[o]; i != -1; i = records[i].he) { if (!strcmp(records[i].ID, ID)) { records[i].distance= records[i].distance+distance; return 0; } } records[count].distance = records[count].distance+distance; strncpy(records[count].ID, ID, 18); records[count].he = zzt[o]; zzt[o] = count++; }对每一个指令添加注释
时间: 2024-03-11 13:44:14 浏览: 72
// 定义哈希表的大小
#define Tablesize 1000005
// 定义哈希表数组和计数器
int zzt[Tablesize], count;
// 插入函数,用于将 ID 和 distance 插入哈希表中
void insert(char *ID, int distance);
// 定义记录结构体
struct Record {
char ID[19];
int distance;
int he;
}Record;
// 定义记录结构体数组
struct Record records[Tablesize];
// 哈希函数,用于将 ID 转化为哈希表中的下标
int hash(char *ID);
// 查找函数,用于在哈希表中查找 ID 并返回其在记录结构体数组中的下标
int find(char *ID);
// 主函数
int main() {
// 定义变量
char ID[19];
int n, k, m, distance;
// 初始化哈希表数组
for (int i = 0; i < sizeof(zzt) / sizeof(zzt[0]); i++) {
zzt[i] = -1;
}
// 输入 n 和 k,分别代表 ID 的数量和最小距离
scanf("%d %d", &n, &k);
// 循环读入 ID 和 distance,并调用 insert 函数将其插入哈希表中
for (int i = 0; i < n; i++) {
scanf("%s %d", ID, &distance);
if (distance < k) distance = k;
insert(ID, distance);
}
// 输入 m,代表要查询的 ID 数量
scanf("%d", &m);
// 循环读入要查询的 ID,并调用 find 函数在哈希表中查找其在记录结构体数组中的下标
int t = -1;
for (int i = 0; i < m; i++) {
scanf("%s", ID);
t = find(ID);
if (t == -1) printf("No Info\n");
else printf("%d\n", records[t].distance);
}
}
// 查找函数,用于在哈希表中查找 ID 并返回其在记录结构体数组中的下标
int find(char *ID) {
int h = hash(ID);
for (int i = zzt[h]; i != -1; i = records[i].he)
if (!strcmp(records[i].ID, ID)) return i;
return -1;
}
// 哈希函数,用于将 ID 转化为哈希表中的下标
int hash(char *ID) {
long int yy = 0;
for (int i = 0; i < 17; i++)
yy = 10 * yy + (ID[i] - '0');
return yy%Tablesize;
}
// 插入函数,用于将 ID 和 distance 插入哈希表中
void insert(char *ID, int distance) {
int o = hash(ID);
for (int i = zzt[o]; i != -1; i = records[i].he) {
if (!strcmp(records[i].ID, ID)) {
records[i].distance= records[i].distance+distance;
return 0;
}
}
records[count].distance = records[count].distance+distance;
strncpy(records[count].ID, ID, 18);
records[count].he = zzt[o];
zzt[o] = count++;
}
阅读全文