[CENTER][CENTER][CENTER]Đây là cách làm của mình, bạn thử xem sao.
Mặc định là nó vẽ theo chiều kim đồng hồ. Nếu muốn tổng quát hơn thì sửa lạ 1 tí thôi!
Còn về ma trận chuyển vị thì có khi cậu phải hỏi anh Google thui !!
Nhớ chạy bằng Free Pascal nhá, chạy bằng TURBO là Die lun :)))
Program goesaround;
Uses Crt;
var
a:array[0..1000,0..1000] of integer;
i,j,size,max:integer;
n:integer;
Procedure intial( i1,j1:shortint ;time:integer);
begin
While time<max do
Begin
if a[i,j]+1=n then break;
inc(time);
i:=i+i1;
j:=j+j1;
a[i,j]:=a[i-i1,j-j1]+1;
End;
end;
Procedure findsize;
var
r:real;
begin r:=sqrt(n);
If r=trunc(r) then
size:=trunc(r)
else
size:=trunc(r)+1;
if size mod 2=0 then
inc(size);
i:= size div 2 +1;
j:= i;
a[i,j]:=1;
End;
Procedure Print;
Var
i,j:integer;
Begin
for i:=1 to size do
Begin
for j:=1 to size do
if a[i,j]=0 then
write(' ':4)
else
write(a[i,j]:4);
writeln;
writeln;
end;
End;
begin
clrscr;
write(' Insert Number please ');
Readln( n );
findsize;
max:=0;
while max+1<>size do
Begin
inc(j);
inc(max,2);
a[i,j]:=a[i,j-1]+1;
intial(1,0,1);
intial(0,-1,0);
intial(-1,0,0);
intial(0,1,0);
end;
print;
readln;
End.
Bookmarks