/
Pereroncino
/
Proga
Обзор
Документация
Войти
/
Pereroncino
/
Proga
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
lab9(4).cpp
70 строк
2 KB
Pereroncino
create: lab9(1).cpp, lab9(2).cpp, lab9(3).cpp, lab9(4).cpp
11 май 2026, 14:12
Верифицирован
11 май 2026, 14:12
d3065f4
Код
Авторство
О чём код?
#include <stdio.h> #include <string.h> #include <stdlib.h> #define M 17 #define N 12 struct Student { char name[80]; int grades[4]; int occupied; }; Student ht[M]; unsigned int h(char *str) { unsigned int hash = 0, pol = 31; for (int i = 0; str[i]; i++) { hash = (hash * pol + str[i]) % M; } return hash % M; } int add_to_hash(char *name, int *g, int &coll) { coll = 0; int hash = h(name); while (ht[hash].occupied) { if (strcmp(ht[hash].name, name) == 0) return hash; hash = (hash + 1) % M; coll++; } strcpy(ht[hash].name, name); for (int i = 0; i < 4; i++) ht[hash].grades[i] = g[i]; ht[hash].occupied = 1; return hash; } int find_in_hash(char *name) { int hash = h(name); int start = hash; while (ht[hash].occupied) { if (strcmp(ht[hash].name, name) == 0) return hash; hash = (hash + 1) % M; if (hash == start) break; } return -1; } int main() { int total_coll = 0, c; char names[N][80] = {"Ivanov", "Petrov", "Sidorov", "Popov", "Smirnov", "Kozlov", "Novikov", "Morozov", "Volkov", "Lebedev", "Egorov", "Pavlov"}; int g[4] = {4, 4, 4, 4}; for (int i = 0; i < M; i++) ht[i].occupied = 0; printf("%-5s %-15s %-10s\n", "Ind", "Name", "Collisions"); for (int i = 0; i < N; i++) { int index = add_to_hash(names[i], g, c); total_coll += c; printf("%-5d %-15s %-10d\n", index, names[i], c); } printf("\nSrednee kol-vo kolliziy: %.2f\n", (float)total_coll / N); char test_name[] = "Volkov"; int res = find_in_hash(test_name); if (res != -1) printf("Nayden v yacheike: %d\n", res); return 0; }