10. 227
}
for (i=1;i<=m1;i++)
for (j=1;j<=h1;j++)
{
a[i][h2+j]=0.0;
if (i==j)
a[i][h2+j]=1.0;
}
it=0;
t=1;
while (t)
{
it=it+1;
if (it<(m*n*5))
{
mi=a[m1][1];
ps=1;
for (j=2;j<=n1-1;j++)
if (a[m1][j]<mi)
{
mi=a[m1][j];
ps=j;
}
if (mi>-0.00001)
{
z=a[m1][n1];
t=0;
}
mi=1e+20;
pz=0;
for (i=1;i<=m1-1;i++)
{
if (a[i][ps]<=0.0)
continue;
h=a[i][n1]/a[i][ps];
if (h<mi)
{
mi=h;
pz=i;
}
}
if (pz==0)
{
if (p==2)
{
printf("Khong ton tai nghiemn");
t=0;
11. 228
}
else
{
printf("Nghiem khong bi gioi hann");
t=0;
}
}
if (p==1)
bv[pz]=ps;
hi=a[pz][ps];
for (j=1;j<=n1;j++)
a[pz][j]=a[pz][j]/hi;
if (pz!=1)
for (i=1;i<=pz-1;i++)
{
hi=a[i][ps];
for (j=1;j<=n1;j++)
a[i][j]=a[i][j]-hi*a[pz][j];
}
for (i=pz+1;i<=m1;i++)
{
hi=a[i][ps];
for (j=1;j<=n1;j++)
a[i][j]=a[i][j]-hi*a[pz][j];
}
}
else
printf("Nghiem bat thuong");
}
}
void main()
{
clrscr();
printf("PHUONG PHAP DON HINHn");
printf("n");
flushall();
printf("Cho bai toan tim max(1) hay min(2)(1/2)? : ");
scanf("%d",&p);
printf("Cho so bien n = ");
scanf("%d",&n);
printf("Cho so dieu kien bien m = ");
scanf("%d",&m);
n1=n+m+1;
if (p==2)
m1=n+1;
else
12. 229
m1=m+1;
printf("Cho ma tran cac dieu kien bienn");
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
if (p==2)
{
printf("a[%d][%d] = ",i,j);
scanf("%f",&a[j][i]);
}
else
{
printf("a[%d][%d] = ",i,j);
scanf("%f",&a[i][j]);
}
printf("n");
printf("Cho ma tran ve phain");
for (i=1;i<=m;i++)
if (p==2)
{
printf("b[%d] = ",i);
scanf("%f",&a[m1][i]);
}
else
{
printf("b[%d] = ",i);
scanf("%f",&a[i][n1]);
}
printf("n");
printf("Cho ham muc tieun");
for (j=1;j<=n;j++)
if (p==2)
{
printf("z[%d] = ",j);
scanf("%f",&a[j][n1]);
}
else
{
printf("z[%d] = ",j);
scanf("%f",&a[m1][j]);
}
if (p==2)
hi=m;
else
hi=n;
for (j=1;j<=hi;j++)
a[m1][j]=-a[m1][j];
a[m1][n1]=0.0;
18. 235
c[a[k][2]]=0.0;
goto sau;
}
}
}
k2=m-1;
r1=p[k2][1];
c1=p[k2][2];
k3=l;
k=1;
s=1;
bon: if (k==1)
{
z[k][1]=r1;
z[k][2]=c1;
k=k+1;
goto bon;
}
else
{
if (s==1)
{
for (j=1;j<=k3;j++)
if (a[j][2]==c1)
{
r1=a[j][1];
s=2;
z[k][1]=r1;
z[k][2]=c1;
k=k+1;
goto bon;
}
k=k-1;
}
else
{
for (j=1;j<=k2;j++)
if (p[j][1]==r1)
{
c1=p[j][2];
s=1;
z[k][1]=r1;
z[k][2]=c1;
k=k+1;
goto bon;
}
else
19. 236
continue;
k=k-1;
}
}
k5=1;
nam: if (k5==k)
{
l=l+1;
a[l][1]=z[k][1];
a[l][2]=z[k][2];
if (l!=n)
{
for (i=1;i<=n;i++)
{
r[i]=0.0;
c[i]=0.0;
p[i][1]=0;
p[i][2]=0;
}
for (i=1;i<=l;i++)
c[a[i][2]]=1.0;
m=1;
goto hai;
sau: m1=9999;
for (i=1;i<=n;i++)
if (r[i]==0.0)
for (j=1;j<=n;j++)
if (c[j]==0.0)
if (x[i][j]<m1)
m1=x[i][j];
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
if ((r[i]!=0.0)||(c[j]!=0.0))
if ((r[i]!=1.0)||(c[j]!=1.0))
continue;
else
x[i][j]=x[i][j]+m1;
else
x[i][j]=x[i][j]-m1;
}
goto hai;
}
}
else
{
for (i=1;i<=l;i++)