Guest User

Untitled

a guest
Apr 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <stdio.h>
  3. using namespace std;
  4. int N = 4;
  5. int* BASE = new int[N + 1];
  6. int* TOP = new int[N];
  7. int* OLDTOP = new int[N];
  8. int* NEWBASE = new int[N];
  9. int* CONTENTS = new int[20];
  10. int* D = new int[N];
  11. int INC = 0;
  12. float T(float a, float b, float q, int d) {
  13. return q + a + d*b;
  14. }
  15. int toInt(float i) {
  16. return (int) i;
  17. }
  18. //R3/R5 - ïåðåìåùåíèå ñïèñêà âíèç/ââåðõ
  19. void moving(int j, int direction) {
  20. int q = direction * (NEWBASE[j] - BASE[j]);
  21. if (direction < 0) {
  22. for (int l = BASE[j] + 1; l <= TOP[j]; l++) {
  23. CONTENTS[l + q * direction] = CONTENTS[l];
  24. CONTENTS[l] = 0;
  25. }
  26. } else {
  27. for (int l = TOP[j]; l >= BASE[j] + 1; l--) {
  28. CONTENTS[l + q * direction] = CONTENTS[l];
  29. CONTENTS[l] = 0;
  30. }
  31. }
  32. BASE[j] = NEWBASE[j];
  33. TOP[j] = TOP[j] + direction*q;
  34. }
  35. //R2/R4 - ïîèñê íà÷àëà ïåðåìåùåíèÿ âíèç/ââåðõ
  36. void searchBeganMoving(int j, int direction) {
  37. int q = 0;
  38. while (1<2) {
  39. j += direction;
  40. if (j > N - 1) {
  41. searchBeganMoving(j, -direction);
  42. return;
  43. }
  44. if (j == 0 && direction < 0) {
  45. //Ïðåêðàùåíèå àëãîðèòìà
  46. return;
  47. }
  48. if (direction * NEWBASE[j] < BASE[j] * direction) {
  49. moving(j, -direction);
  50. }
  51. }
  52. }
  53. void algR() {
  54. searchBeganMoving(0, 1);
  55. }
  56. int getNewBase(int index, float t, float q) {
  57. return (NEWBASE[index - 1] + TOP[index - 1] - BASE[index - 1] + toInt(t) - toInt(q));
  58. }
  59. void algG(int index) {
  60. int sum = 20;
  61. for (int i = 0; i < 4; i++) {
  62. sum -= (TOP[i] - BASE[i]);
  63. D[i] = TOP[i] - OLDTOP[i];
  64. INC += D[i];
  65. }
  66. if (sum > 0) {
  67. float a = 0.1 * sum / 4;
  68. float b = 0.9 * sum / INC;
  69. NEWBASE[0] = BASE[0];
  70. float t = 0, q = 0;
  71. for (int j = 1; j < 4; j++) {
  72. t = T(a, b, q, D[j - 1]);
  73. NEWBASE[j] = getNewBase(j, t, q);
  74. q = t;
  75. }
  76. --TOP[index];
  77. algR();
  78. ++TOP[index];
  79. CONTENTS[TOP[index]] = index + 1;
  80. } else {
  81. //Ðàáîòà íå ìîæåò áûòü ïðîäîëæåíà
  82. printf("Ðàáîòà íå ìîæåò áûòü ïðîäîëæåíà\n");
  83. return;
  84. }
  85. }
  86. void addToStack(int indexOfStack) {
  87. TOP[indexOfStack] += 1;
  88. if (TOP[indexOfStack] > BASE[indexOfStack + 1]) {
  89. algG(indexOfStack);
  90. } else {
  91. CONTENTS[TOP[indexOfStack]] = indexOfStack + 1;
  92. }
  93. }
  94. int main(int argc, char** argv) {
  95. //Óñòàíàâëèâàåì íà÷àëüíûå ïàðàìåòðû
  96. BASE[0] = -1;
  97. BASE[1] = 2;
  98. BASE[2] = 6;
  99. BASE[3] = 8;
  100. OLDTOP[0] = TOP[0] = 0;
  101. OLDTOP[1] = TOP[1] = 3;
  102. OLDTOP[2] = TOP[2] = 6;
  103. OLDTOP[3] = TOP[3] = 8;
  104. BASE[4] = 19;
  105. CONTENTS[0] = 1;
  106. CONTENTS[3] = 2;
  107. // âûïîëíÿåì àëãîðèòìû I4I3I3I3
  108. addToStack(3);
  109. addToStack(2);
  110. addToStack(2);
  111. addToStack(2);
  112. //Ðåçóëüòàò
  113. for (int i = 0; i < N; i++) {
  114. printf("Base[%i] = %i Top[%i] = %i \n",i, BASE[i],i,TOP[i]);
  115. }
  116. printf("\n\nÊîíå÷íûé ðåçóëüòàò:\n");
  117. for (int i = 0; i < 20; i++) {
  118. printf("%i ", CONTENTS[i]);
  119. }
  120. printf("\n\n\n");
  121. return 0;
  122. }
Add Comment
Please, Sign In to add comment