Guest User

Untitled

a guest
Apr 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. /*
  2. * File: main.cpp
  3. * Author: Íèêèòà
  4. *
  5. * Created on 30 Ноябрь 2011 г., 11:51
  6. */
  7.  
  8. #include <cstdlib>
  9. #include <stdio.h>
  10.  
  11. using namespace std;
  12.  
  13. /*
  14. *
  15. */
  16. int N = 4;
  17. int* BASE = new int[N + 1];
  18. int* TOP = new int[N];
  19. int* OLDTOP = new int[N];
  20. int* NEWBASE = new int[N];
  21. int* CONTENTS = new int[20];
  22. int* D = new int[N];
  23. int INC = 0;
  24.  
  25. float T(float a, float b, float q, int d) {
  26. return q + a + d*b;
  27. }
  28.  
  29. int toInt(float i) {
  30. return (int) i;
  31. }
  32.  
  33. //R3/R5 - перемещение списка вниз/вверх
  34.  
  35. void moving(int j, int direction) {
  36. int q = direction * (NEWBASE[j] - BASE[j]);
  37.  
  38. if (direction < 0) {
  39. for (int l = BASE[j] + 1; l <= TOP[j]; l++) {
  40. CONTENTS[l + q * direction] = CONTENTS[l];
  41. CONTENTS[l] = 0;
  42. }
  43. } else {
  44. for (int l = TOP[j]; l >= BASE[j] + 1; l--) {
  45. CONTENTS[l + q * direction] = CONTENTS[l];
  46. CONTENTS[l] = 0;
  47. }
  48. }
  49. BASE[j] = NEWBASE[j];
  50. TOP[j] = TOP[j] + direction*q;
  51.  
  52.  
  53. }
  54.  
  55.  
  56. //R2/R4 - поиск начала перемещения вниз/вверх
  57.  
  58. void searchBeganMoving(int j, int direction) {
  59. int q = 0;
  60. while (2 < 3) {
  61. j += direction;
  62. if (j > N - 1) {
  63. searchBeganMoving(j, -direction);
  64. return;
  65. }
  66. if (j == 0 && direction < 0) {
  67. //Прекращение алгоритма
  68. return;
  69. }
  70. if (direction * NEWBASE[j] < BASE[j] * direction) {
  71. moving(j, -direction);
  72. }
  73. }
  74. }
  75.  
  76. void algR() {
  77. searchBeganMoving(0, 1);
  78. }
  79.  
  80. int getNewBase(int index, int t, int q) {
  81. return (NEWBASE[index - 1] + TOP[index - 1] - BASE[index - 1] + toInt(t) - toInt(q));
  82. }
  83.  
  84. void algG(int index) {
  85. int sum = 20;
  86. for (int i = 0; i < 4; i++) {
  87. sum -= (TOP[i] - BASE[i]);
  88. D[i] = TOP[i] - OLDTOP[i];
  89. INC += D[i];
  90. }
  91.  
  92.  
  93. if (sum > 0) {
  94. float a = 0.1 * sum / 4;
  95. float b = 0.9 * sum / INC;
  96. NEWBASE[0] = BASE[0];
  97. float t = 0, q = 0;
  98. for (int j = 1; j < 4; j++) {
  99. t = T(a, b, q, D[j - 1]);
  100. NEWBASE[j] = getNewBase(j, t, q);
  101. q = t;
  102.  
  103. }
  104. --TOP[index];
  105. algR();
  106. ++TOP[index];
  107. CONTENTS[TOP[index]] = index + 1;
  108. } else {
  109. //Работа не может быть продолжна
  110. printf("Работа не может быть продолжена\n");
  111. return;
  112. }
  113.  
  114. }
  115.  
  116. void addToStack(int indexOfStack) {
  117. TOP[indexOfStack] += 1;
  118.  
  119. if (TOP[indexOfStack] > BASE[indexOfStack + 1]) {
  120. algG(indexOfStack);
  121. } else {
  122. CONTENTS[TOP[indexOfStack]] = indexOfStack + 1;
  123. }
  124.  
  125.  
  126. }
  127.  
  128. int main(int argc, char** argv) {
  129. //Устанавливаем начальные параметры
  130. BASE[0] = -1;
  131. BASE[1] = 1;
  132. BASE[2] = 4;
  133. BASE[3] = 5;
  134. OLDTOP[0] = TOP[0] = 0;
  135. OLDTOP[1] = TOP[1] = 1;
  136. OLDTOP[2] = TOP[2] = 4;
  137. OLDTOP[3] = TOP[3] = 6;
  138. BASE[4] = 19;
  139. CONTENTS[0] = 1;
  140. CONTENTS[6] = 4;
  141.  
  142. // выполняем алгоритмы I2,I3,I1,I4,I2,I3
  143. addToStack(1);
  144. addToStack(2);
  145. addToStack(0);
  146. addToStack(3);
  147. addToStack(1);
  148. addToStack(2);
  149.  
  150.  
  151. //Результат
  152. for (int i = 0; i < N; i++) {
  153. printf("Base[%i] = %i Top[%i] = %i \n",i, BASE[i],i,TOP[i]);
  154. }
  155. printf("\n\nКонечный результат:\n");
  156. for (int i = 0; i < 20; i++) {
  157. printf("%i ", CONTENTS[i]);
  158. }
  159. printf("\n\n\n");
  160. return 0;
  161. }
Add Comment
Please, Sign In to add comment