Guest User

Untitled

a guest
Dec 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define SIZE_RING 11
  7.  
  8. const int ring[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  9.  
  10. typedef struct tnode {
  11. int spectVector[SIZE_RING];
  12. struct tnode *left;
  13. struct tnode *right;
  14. }SpectrNode_t;
  15.  
  16. int CompareIntArrays(int arr1[SIZE_RING], int arr2[SIZE_RING]);
  17. void generatePerms(int arr[SIZE_RING], SpectrNode_t **head);
  18. void countSpectVector(SpectrNode_t **head, int perm[SIZE_RING]);
  19. void find (SpectrNode_t **head, int newSpectr[SIZE_RING]);
  20. SpectrNode_t *addTree(SpectrNode_t *p, int spectrVector[SIZE_RING])
  21. {
  22. int i = 0;
  23. int cond = 0, tmp_rand = 0;
  24. srand(time(NULL));
  25. if (p == NULL) {
  26. p = (SpectrNode_t *)malloc(sizeof(SpectrNode_t));
  27. for (i = 0; i < SIZE_RING; i++)
  28. p->spectVector[i] = spectrVector[i];
  29. p->left = p->right = NULL;
  30. }
  31. else if (CompareIntArrays(p->spectVector,spectrVector) == 1) {
  32. tmp_rand = rand();
  33. if (tmp_rand % 2 == 0)
  34. p->left = addTree(p->left,spectrVector);
  35. if (tmp_rand % 2 == 1)
  36. p->right = addTree(p ->right,spectrVector);
  37. }
  38. return p;
  39. }
  40.  
  41. int main()
  42. {
  43. SpectrNode_t *root = NULL;
  44. int i = 0, *perm;
  45. perm = (int*)malloc(SIZE_RING * sizeof(int));
  46. for (i = 0; i < SIZE_RING; i++)
  47. perm[i] = i;
  48. generatePerms(perm, &root);
  49. printf("Press any key...");
  50. getchar();
  51. }
  52. int CompareIntArrays(int arr1[SIZE_RING], int arr2[SIZE_RING])
  53. {
  54. int res = 0;
  55. register int i = 0;
  56. for (i = 0; i < SIZE_RING; i++)
  57. if (arr1[i] != arr2[i])
  58. {
  59. res = 1;
  60. break;
  61. }
  62. return res;
  63. }
  64. void generatePerms(int arr[SIZE_RING], SpectrNode_t **head)
  65. {
  66. int c[SIZE_RING];
  67. register int i;
  68.  
  69. i = 0;
  70. memset(c, 0, SIZE_RING * sizeof(int));
  71.  
  72. while (i < SIZE_RING) {
  73. if (c[i] < i) {
  74. if (i % 2 == 0)
  75. swap(&arr[0], &arr[i]);
  76. else{
  77. swap(&arr[c[i]], &arr[i]);
  78.  
  79. }
  80. countSpectVector(head, arr);
  81. c[i] += 1;
  82. i = 0;
  83. }
  84. else {
  85. c[i] = 0;
  86. i += 1;
  87. }
  88. }
  89. }
  90. void countSpectVector(SpectrNode_t **head, int perm[SIZE_RING])
  91. {
  92. int intSpectr[SIZE_RING];
  93. register int i = 0, k = 0;
  94. register int diffTmp = 0, betta = 0;
  95.  
  96. memset(intSpectr, 0, SIZE_RING * sizeof(int));
  97.  
  98. for (i = 0; i < SIZE_RING; i++){
  99. betta = ring[i];
  100. diffTmp = perm[( 1 + betta ) % SIZE_RING] - perm[betta];
  101. if (diffTmp < 0)
  102. diffTmp += SIZE_RING;
  103. intSpectr[diffTmp]++;
  104. }
  105. find(head, intSpectr);
  106. }
  107. void find (SpectrNode_t **head, int newSpectr[SIZE_RING])
  108. {
  109. SpectrNode_t *cur = *head;
  110. int res = 0;
  111. while (cur != NULL) {
  112. if (CompareIntArrays(cur->spectVector, newSpectr) == 0) {
  113. res = 1; // такой элемент уже есть в списке
  114. break;
  115. }
  116. find(cur->left, newSpectr);
  117. find(cur->right, newSpectr);
  118. }
  119. cur = *head;
  120. if (res == 0) {
  121. addTree(head, newSpectr);
  122. }
  123. }
Add Comment
Please, Sign In to add comment