#include <iostream>
using namespace std;
void print(double A[1000][1000], double B[1000], double z[1000], int var, int con) {
for (int i = 0; i <= var; i++)
cout << z[i] << " ";
cout << endl;
for (int i = 0; i < con; i++) {
for (int j = 0; j < var; j++) {
cout << A[i][j] << " ";
}
cout << B[i] << endl;
}
cout << endl;
}
void convert(double a[1000][1000], double b[1000], double z[1000], int &var, int &con) {
z[var + con] = z[var];
for (int i = 0; i < con; i++) {
a[i][var + i] = 1;
z[var + i] = 0;
}
var += con;
}
int check(double a[1000], int n) {
bool zero = 0;
for (int i = 0; i < n; i++)
if (a[i] < 0)
return 3;
else if (a[i] == 0)
zero = 1;
return (zero) ? 2 : 1;
}
int posSelected(double a[1000], int n, int type) {
if (check(a, n) != 3)
return -1;
if (type == 2) {
for (int i = 0; i < n; i++)
if (a[i] < 0)
return i;
}
int minPos = -1, minVar = 0;
for (int i = n - 1; i >= 0; i--)
if ((a[i] < minVar)) {
minVar = a[i];
minPos = i;
}
return minPos;
}
void spin(double a[1000][1000], double b[1000], double z[1000], int var, int con, int type) {
int inPos, outPos;
if (type == 3) {
inPos = var - 1;
outPos = posSelected(b, con, type);
}
else {
inPos = posSelected(z, var, type);
double miVal = 1e9;
outPos = -1;
for (int i = 0; i < con; i++)
if (a[i][inPos] > 0) {
double temp = b[i] / a[i][inPos];
if (temp < miVal) {
miVal = temp;
outPos = i;
}
}
}
double di = - z[inPos] / a[outPos][inPos];
for (int i = 0; i < var; i++)
z[i] += di * a[outPos][i];
z[var] += di * b[outPos];
for (int i = 0; i < con; i++)
if (i != outPos) {
di = - a[i][inPos] / a[outPos][inPos];
for (int j = 0; j < var; j++)
a[i][j] += di * a[outPos][j];
b[i] += di * b[outPos];
}
cout << inPos+1 << " " << outPos+1 << " " << type << endl;
print(a, b, z, var, con);
}
void repeat(double a[1000][1000], double b[1000], double z[1000], int var, int con) {
convert(a, b, z, var, con);
print(a, b, z, var, con);
int type = check(b, con);
if (type == 3) {
double pha[1000];
for (int i = 0; i < var; i++)
pha[i] = 0;
for (int i = 0; i < con; i++)
a[i][var] = 1;
pha[var + 1] = 0;
pha[var] = 1;
var++;
spin(a, b, pha, var, con, type);
type = 1;
while (check(pha, var) == 3)
spin(a, b, pha, var, con, type);
bool nextSpin = 0;
if ((pha[var - 1] == 1) && (pha[var] == 0)) {
nextSpin = 1;
for (int i = 0; i < var - 1; i++)
if (a[i] != 0) {
nextSpin = 0;
break;
}
}
if (!nextSpin) {
return;
}
var--;
for (int i = 0; i < con; i++)
a[i][var] = 0;
}
while (check(z, var) == 3) {
spin(a, b, z, var, con, type);
}
}
int main() {
int var = 3, con = 4;
double A[1000][1000] = {{1, -5, 1},
{2, 2, 0},
{-1, 2, 0},
{-1, 5, -1}};
double B[1000] = {6, 7, 5, -6};
double z[1000] = {-2, -3, -1, 0};
print(A, B, z, var, con);
repeat(A, B, z, var, con);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKdm9pZCBwcmludChkb3VibGUgQVsxMDAwXVsxMDAwXSwgZG91YmxlIEJbMTAwMF0sIGRvdWJsZSB6WzEwMDBdLCBpbnQgdmFyLCBpbnQgY29uKSB7Cglmb3IgKGludCBpID0gMDsgaSA8PSB2YXI7IGkrKykKICAgICAgICBjb3V0IDw8IHpbaV0gPDwgIiAiOwogICAgY291dCA8PCBlbmRsOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBjb247IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgdmFyOyBqKyspIHsKICAgICAgICAgICAgY291dCA8PCBBW2ldW2pdIDw8ICIgIjsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBCW2ldIDw8IGVuZGw7CiAgICB9CiAgICBjb3V0IDw8IGVuZGw7Cn0KIAp2b2lkIGNvbnZlcnQoZG91YmxlIGFbMTAwMF1bMTAwMF0sIGRvdWJsZSBiWzEwMDBdLCBkb3VibGUgelsxMDAwXSwgaW50ICZ2YXIsIGludCAmY29uK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