标签:
小明玩一个数字游戏,取个n行n列数字矩阵(其中n为不超过100的奇数),数字的填补方法为:在矩阵中心从1开始以逆时针方向绕行,逐圈扩大,直到n行n列填满数字,请输出该n行n列正方形矩阵以及其的对角线数字之和.
n(即n行n列)
n+1行,n行为组成的矩阵,最后一行为对角线数字之和
3
5 4 3
6 1 2
7 8 9
25
1 var a,c:array[-100..100,-100..100]of longint; 2 i,j,fx,n,k,num,x,y,tw,ans:longint; 3 begin 4 readln(n); 5 a[0,0]:=1; 6 k:=1; 7 num:=2; 8 x:=0; 9 y:=0; 10 fx:=0; 11 while k<>n do 12 begin 13 if (tw mod 2=0)and(tw<>0) then inc(k); 14 for i:=1 to k do 15 begin 16 if fx=0 then 17 begin 18 inc(y); 19 a[x,y]:=num; 20 inc(num); 21 end; 22 if fx=1 then 23 begin 24 dec (x); 25 a[x,y]:=num; 26 inc(num); 27 end; 28 if fx=2 then 29 begin 30 dec(y); 31 a[x,y]:=num; 32 inc(num); 33 end; 34 if fx=3 then 35 begin 36 inc(x); 37 a[x,y]:=num; 38 inc(num); 39 end; 40 end; 41 inc(tw); 42 inc(fx); 43 fx:=fx mod 4; 44 end; 45 46 x:=0; y:=0; 47 if n mod 2 <>0 then 48 for i:=-n div 2 to n div 2 do 49 begin 50 inc(x); 51 y:=1; 52 for j:= -n div 2 to n div 2 do 53 begin 54 c[x,y]:=a[i,j]; 55 inc(y); 56 end; 57 writeln; 58 end; 59 60 if n mod 2=0 then 61 for i:=2-n div 2-2 to n div 2-1 do 62 begin 63 inc(x); 64 y:=1; 65 for j:=-n div 2+1 to n div 2 do 66 begin 67 c[x,y]:=a[i,j]; 68 inc(y); 69 70 71 end; 72 writeln; 73 end; 74 for i:=1 to n do 75 begin 76 for j:=1 to n do 77 begin 78 write(c[i,j],‘ ‘); 79 if (i=j) or(i+j=n) then 80 inc(ans,c[i,j]); 81 end; 82 writeln; 83 end; 84 85 writeln(ans); 86 end.
标签:
原文地址:http://www.cnblogs.com/liumengyue/p/4986774.html