Advertisement
Guest User

Untitled

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