Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 18 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
18
Dung lượng
61,5 KB
Nội dung
Chương trình mẫu matrậnđiềuhoà chạy tự động trên máy tính viếtbằ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 Clrscr; Writeln(‘Day la chuong trinh sap xep cac so nguyen tu 1 den n*n vao mot ma tran’); Writeln(‘n*n sao cho tong cac hang, cac cot va cac duong cheo deu bang nhau :’); Write(‘Ban hay chon n tu 3 den 100 : ’); Readln(n); Nhap_matran; If (n mod 2)<>0 then begin k:=(n+1) div 2; If (k mod 2)=0 then begin {n=3,7,11,…} Xoay_1; m:=(n-3) div 4; If m<>0 then begin Hoanvi_ngang(m); Hoanvi_doc(m); end; end else begin {n=5,9,13,…} Xoay_2; m:=(n-5) div 4; If m<>0 then Hoanvi_ngang(m); Hoanvi_doc(m+1); end; end else begin k:=n div 2; Xoay_3; If (k mod 2)=0 then begin {n=4,8,12,…} m:=(n-4) div 4; If m<>0 then begin Hoanvi_ngang(m); Hoanvi_doc(m); end; end else begin {n=6,10,14,…} m:=(n-6) div 4; If m<>0 then begin Hoanvi_ngang(m); Hoanvi_doc(m); end; Hoanvi_don(m+1); end; end; Writeln(‘Ma tran cua ban la :’); Xuat_matran; Readln; END. Dưới đây là chương trình mẫu để chạy tự động trên máy tính viếtbằ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]; [...]... If (k mod 2)=0 then begin {n=3,7,11,…} Xoay_1; m:=(n-3) div 4; If m0 then begin Hoanvi_ngang(m); Hoanvi_doc(m); end; end else begin {n=5,9,13,…} Xoay_2; m:=(n-5) div 4; If m0 then Hoanvi_ngang(m); Hoanvi_doc(m+1); end; end else begin k:=n div 2; Xoay_3; If (k mod 2)=0 then begin {n=4,8,12,…} m:=(n-4) div 4; If m0 then begin Hoanvi_ngang(m); Hoanvi_doc(m); end; end else begin {n=6,10,14,…} m:=(n-6)... 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 Clrscr; Writeln(‘Day la chuong trinh sap xep cac so nguyen tu 1 den n*n vao mot ma tran’); Writeln(‘n*n sao cho tong cac hang, cac cot va cac duong cheo deu bang nhau :’); Write(‘Ban hay chon n tu 3 den 100 : ’); Readln(n); Nhap_matran; If (n mod 2)0 then begin k:=(n+1) div... {n=4,8,12,…} m:=(n-4) div 4; If m0 then begin Hoanvi_ngang(m); Hoanvi_doc(m); end; end else begin {n=6,10,14,…} m:=(n-6) div 4; If m0 then begin Hoanvi_ngang(m); Hoanvi_doc(m); end; Hoanvi_don(m+1); end; end; Writeln( Matran cua ban la :’); Xuat_matran; Readln; END ... 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)... 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)... 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... 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; . 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;. m<>0 then begin Hoanvi_ngang(m); Hoanvi_doc(m); end; Hoanvi_don(m+1); end; end; Writeln( Ma tran cua ban la :’); Xuat_matran; Readln; END. Dưới đây là