Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace new_lr_1_var_12 {
  7. class Program {
  8. const int m = 44;
  9. const int n = 4;
  10.  
  11. static int HashFunction(int key) {
  12. return Convert.ToInt16(key % Convert.ToInt16(1.5 * m));
  13. }
  14. static void Main(string[] args) {
  15. int t = Convert.ToInt16(1.5 * m);
  16. Random rnd = new Random();
  17. int[] keys = new int[t];
  18. Console.Write(" Исходные ключи: \n");
  19. for (int i = 0; i < m; ++i) {
  20. keys[i] = rnd.Next(Convert.ToInt16(Math.Pow(10, n - 1)), Convert.ToInt16(Math.Pow(10, n)));
  21. Console.Write("key=" + keys[i] + " h(" + keys[i] + ")=" + HashFunction(keys[i]) + " ");
  22. if ((i + 1) % 4 == 0) Console.WriteLine();
  23. }
  24.  
  25. int[] hasht = new int[t];
  26. for (int i = 0; i < t; ++i)
  27. hasht[i] = 0;
  28. int k = m;
  29. for (int j = 0; j < m; ++j) {
  30. if (hasht[HashFunction(keys[j])] == 0) hasht[HashFunction(keys[j])] = keys[j];
  31. else {
  32. for (int i = 1;; ++i) {
  33. ++k;
  34. if (hasht[(HashFunction(keys[j]) + i * i) % t] == 0) {
  35. hasht[(HashFunction(keys[j]) + i * i) % t] = keys[j];
  36. break;
  37. }
  38. }
  39.  
  40. }
  41. }
  42. Console.Write("\n Хеш-таблица: \n");
  43. for (int i = 0; i < t; ++i) {
  44. Console.Write("[" + i + "]:" + hasht[i] + " ");
  45. if ((i + 1) % 7 == 0) Console.WriteLine();
  46. }
  47. Console.Write("\n Коэффициент заполнения = " + Convert.ToDouble(m) / Convert.ToDouble(t));
  48. Console.Write("\n Среднее число проб = " + Convert.ToDouble(k) / Convert.ToDouble(m));
  49. Console.ReadKey();
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement