Chương trình mẫu ma trận điều hoà chạy tự động trên máy tính
viết bằng Pascal
( các bạn có thể chuyển sang ngôn ngữ khác có giao diện đẹp hơn )
Program Matran_dieuhoa;
Uses Crt;
Const L=100;
Var a: Array[1..L,1..L] of Integer;
hang, cot: Array[1..L] of Real;
i, j, k,m, n: 1..L;
tam: Integer; tong: Real;
{---------------------------------------------------------------}
Procedure Nhap_matran;
Begin
For j:=1 to n do begin
tam:=(j-1)*n;
For i:=1 to n do a[i,j]:=i+tam;
end;
End; {Nhap_matran}
{---------------------------------------------------------------}
Procedure Xoay_1; {n=3,7,11,…}
Begin
For i:=1 to (k-1) do begin
tam:=a[i,i];
a[i,i]:=a[i,k];
a[i,k]:=a[n+1-i,i];
a[n+1-i,i]:=a[k,i];
a[k,i]:=a[n+1-i,n+1-i];
a[n+1-i,n+1-i]:= a[n+1-i,k];
a[n+1-i,k]:=a[i,n+1-i];
a[i,n+1-i]:= a[k,n+1-i];
a[k,n+1-i]:=tam;
end;
End; {Xoay_1}
{---------------------------------------------------------------}
Procedure Xoay_2; {n=5,9,1,…}
Begin
For i:=1 to (k-1) do begin
tam:=a[i,i];
a[i,i]:=a[i,k];
a[i,k]:=a[i,n+1-i];
a[i,n+1-i]:=a[k,i];
a[k,i]:=a[n+1-i,n+1-i];
a[n+1-i,n+1-i]:= a[n+1-i,k];
a[n+1-i,k]:=a[n+1-i,i];
a[n+1-i,i]:= a[k,n+1-i];
a[k,n+1-i]:=tam;
end;
End; {Xoay_2}
{---------------------------------------------------------------}
Procedure Xoay_3; {n=4,6,8,…}
Begin
For i:=1 to k do begin
tam:=a[i,i];
a[i,i]:=a[n+1-i,n+1-i];
a[n+1-i,n+1-i]:= tam;
tam:=a[n+1-i,i];
a[n+1-i,i]:= a[i,n+1-i];
a[i,n+1-i]:=tam;
end;
End; {Xoay_3}
{---------------------------------------------------------------}
Procedure Hoanvi_ngang(m: Integer);
Begin
For i:=1 to m do
For j:=1 to (m+1-i) do begin
tam:=a[i,k+j];
a[i,k+j]:= a[i,n+1-k-j];
a[i,n+1-k-j]:=tam;
tam:=a[n+1-i,k+j];
a[n+1-i,k+j]:= a[n+1-i,n+1-k-j];
a[n+1-i,n+1-k-j]:=tam;
end;
If m>1 then for i:=2 to m do
For j:=1 to (i-1) do begin
tam:=a[i,j];
a[i,j]:=a[i,n+1-j];
a[i,n+1-j]:= tam;
tam:=a[n+1-i,j];
a[n+1-i,j]:= a[n+1-i,n+1-j];
a[n+1-i,n+1-j]:=tam;
end;
For i:=(m+1) to (n-k) do
For j:=1 to m do begin
tam:=a[i,i-m-1+j];
a[i,i-m-1+j]:= a[i,n+m+2-i-j];
a[i,n+m+2-i-j]:=tam;
tam:=a[n+1-i,i-m-1+j];
a[n+1-i,i-m-1+j]:= a[n+1-i,n+m+2-i-j];
a[n+1-i,n+m+2-i-j]:=tam;
end;
End;{Hoanvi_ngang}
{---------------------------------------------------------------}
Procedure Hoanvi_doc(m: Integer);
Begin
For j:=1 to m do
For i:=1 to (m+1-j) do begin
tam:=a[k+i,j];
a[k+i,j]:= a[k+i,n+1-j];
a[k+i,n+1-j]:=tam;
tam:=a[k+i,n+1-j];
a[k+i,n+1-j]:= a[n+1-k-i,n+1-j];
a[n+1-k-i,n+1-j]:=tam;
end;
If m>1 then for j:=2 to m do
For i:=1 to (j-1) do begin
tam:=a[i,j];
a[i,j]:=a[n+1-i,j];
a[n+1-i,j]:= tam;
tam:=a[i,n+1-j];
a[i,n+1-j]:= a[n+1-i,n+1-j];
a[n+1-i,n+1-j]:=tam;
end;
For j:=(m+1) to (n-k) do
For i:=1 to m do begin
tam:=a[j-m-1+i,j];
a[j-m-1+i,j]:= a[n+m+2-j-i,j];
a[n+m+2-j-i,j]:=tam;
tam:=a[j-m-1+i,n+1-j];
a[j-m-1+i,n+1-j]:= a[n+m+2-j-i,n+1-j];
a[n+m+2-j-i,n+1-j]:=tam;
end;
End;{Hoanvi_doc}
{---------------------------------------------------------------}
Procedure Hoanvi_don(m: Integer); {n=6,10,14,…}
Begin
{ Hoan vi ngang nua tren }
For i:=1 to m do begin
tam:=a[i,k-m+i];
a[i,k-m+i]:= a[i,k+m+1-i];
a[i,k+m+1-i]:=tam;
end;
For i:=(m+1) to k do begin
tam:=a[i,i-m];
a[i,i-m]:= a[i,n+m+1-i];
a[i,n+m+1-i]:=tam;
end;
{ Hoan vi doc nua trai }
For j:=1 to m do begin
tam:=a[k-m+j,j];
a[k-m+j,j]:= a[k+m+1-j,j];
a[k+m+1-j,j]:=tam;
end;
For j:=(m+1) to k do begin
tam:=a[j-m,j];
a[j-m,j]:= a[n+m+1-j,j];
a[n+m+1-j,j]:=tam;
end;
End; {Hoanvi_don}
{---------------------------------------------------------------}
Procedure Xuat_matran;
Begin
For i:=1 to n do begin
For j:=1 to n do Write(a[i,j]:4);
Writeln;
Writeln;
end;
End; {Xuat_matran}
BEGIN