fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void print(double A[1000][1000], double B[1000], double z[1000], int var, int con) {
  6. for (int i = 0; i <= var; i++)
  7. cout << z[i] << " ";
  8. cout << endl;
  9. for (int i = 0; i < con; i++) {
  10. for (int j = 0; j < var; j++) {
  11. cout << A[i][j] << " ";
  12. }
  13. cout << B[i] << endl;
  14. }
  15. cout << endl;
  16. }
  17.  
  18. void convert(double a[1000][1000], double b[1000], double z[1000], int &var, int &con) {
  19. z[var + con] = z[var];
  20. for (int i = 0; i < con; i++) {
  21. a[i][var + i] = 1;
  22. z[var + i] = 0;
  23. }
  24. var += con;
  25. }
  26.  
  27. int check(double a[1000], int n) {
  28. bool zero = 0;
  29. for (int i = 0; i < n; i++)
  30. if (a[i] < 0)
  31. return 3;
  32. else if (a[i] == 0)
  33. zero = 1;
  34. return (zero) ? 2 : 1;
  35. }
  36.  
  37. int posSelected(double a[1000], int n, int type) {
  38. if (check(a, n) != 3)
  39. return -1;
  40.  
  41. if (type == 2) {
  42. for (int i = 0; i < n; i++)
  43. if (a[i] < 0)
  44. return i;
  45. }
  46.  
  47. int minPos = -1, minVar = 0;
  48. for (int i = n - 1; i >= 0; i--)
  49. if ((a[i] < minVar)) {
  50. minVar = a[i];
  51. minPos = i;
  52. }
  53. return minPos;
  54. }
  55.  
  56. void spin(double a[1000][1000], double b[1000], double z[1000], int var, int con, int type) {
  57. int inPos, outPos;
  58. if (type == 3) {
  59. inPos = var - 1;
  60. outPos = posSelected(b, con, type);
  61. }
  62. else {
  63. inPos = posSelected(z, var, type);
  64. double miVal = 1e9;
  65. outPos = -1;
  66. for (int i = 0; i < con; i++)
  67. if (a[i][inPos] > 0) {
  68. double temp = b[i] / a[i][inPos];
  69. if (temp < miVal) {
  70. miVal = temp;
  71. outPos = i;
  72. }
  73. }
  74. }
  75.  
  76. double di = - z[inPos] / a[outPos][inPos];
  77. for (int i = 0; i < var; i++)
  78. z[i] += di * a[outPos][i];
  79. z[var] += di * b[outPos];
  80.  
  81. for (int i = 0; i < con; i++)
  82. if (i != outPos) {
  83. di = - a[i][inPos] / a[outPos][inPos];
  84. for (int j = 0; j < var; j++)
  85. a[i][j] += di * a[outPos][j];
  86. b[i] += di * b[outPos];
  87. }
  88.  
  89. cout << inPos+1 << " " << outPos+1 << " " << type << endl;
  90. print(a, b, z, var, con);
  91. }
  92.  
  93. void repeat(double a[1000][1000], double b[1000], double z[1000], int var, int con) {
  94. convert(a, b, z, var, con);
  95. print(a, b, z, var, con);
  96. int type = check(b, con);
  97. if (type == 3) {
  98. double pha[1000];
  99. for (int i = 0; i < var; i++)
  100. pha[i] = 0;
  101. for (int i = 0; i < con; i++)
  102. a[i][var] = 1;
  103. pha[var + 1] = 0;
  104. pha[var] = 1;
  105. var++;
  106. spin(a, b, pha, var, con, type);
  107. type = 1;
  108. while (check(pha, var) == 3)
  109. spin(a, b, pha, var, con, type);
  110. bool nextSpin = 0;
  111. if ((pha[var - 1] == 1) && (pha[var] == 0)) {
  112. nextSpin = 1;
  113. for (int i = 0; i < var - 1; i++)
  114. if (a[i] != 0) {
  115. nextSpin = 0;
  116. break;
  117. }
  118. }
  119.  
  120. if (!nextSpin) {
  121. return;
  122. }
  123. var--;
  124. for (int i = 0; i < con; i++)
  125. a[i][var] = 0;
  126. }
  127.  
  128. while (check(z, var) == 3) {
  129. spin(a, b, z, var, con, type);
  130. }
  131. }
  132.  
  133. int main() {
  134. int var = 3, con = 4;
  135. double A[1000][1000] = {{1, -5, 1},
  136. {2, 2, 0},
  137. {-1, 2, 0},
  138. {-1, 5, -1}};
  139. double B[1000] = {6, 7, 5, -6};
  140. double z[1000] = {-2, -3, -1, 0};
  141.  
  142. print(A, B, z, var, con);
  143. repeat(A, B, z, var, con);
  144.  
  145. return 0;
  146. }
Success #stdin #stdout 0.01s 11608KB
stdin
Standard input is empty
stdout
-2 -3 -1 0 
1 -5 1 6
2 2 0 7
-1 2 0 5
-1 5 -1 -6

-2 -3 -1 0 0 0 0 0 
1 -5 1 1 0 0 0 6
2 2 0 0 1 0 0 7
-1 2 0 0 0 1 0 5
-1 5 -1 0 0 0 1 -6

8 4 3
1 -5 1 0 0 0 -1 0 6 
2 -10 2 1 0 0 -1 0 12
3 -3 1 0 1 0 -1 0 13
0 -3 1 0 0 1 -1 0 11
-1 5 -1 0 0 0 1 1 -6

2 4 1
0 0 0 0 0 0 0 1 0 
0 0 0 1 0 0 1 2 0
2.4 0 0.4 0 1 0 -0.4 0.6 9.4
-0.6 0 0.4 0 0 1 -0.4 0.6 7.4
-1 5 -1 0 0 0 1 1 -6