Magic square by darefilz

using C=System.Console;class P{static void Main(){int n=int.Parse(C.ReadLine()),a=1,x=n/2-1,y=n-2;var m=new int[n,n];for(;a<=n*n;++a){if(m[x=(x+1)%n,y=(y+2)%n]!=0){x=(x-1+n)%n;y=(y-1+n)%n;}m[x,y]=a;}for(y=0;y<n;++y){C.Write("{0,3}",m[0,y]);for(x=1;x<n;++x)C.Write(" {0,3}",m[x, y]);C.WriteLine();}}}

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page