自己编的魔方阵程序
#include<stdio.h>#define MAX 30
int square[MAX][MAX];
int N;
void magic();
void main()
{
int i,j;
do
{
printf("please enter odd matrix size:");
scanf("%d",&N);
if(N%2==0||N<=0||N>30)
printf("should be>0 and<30 odd number");
else
break;
}while(1);
magic();
printf("\nthe %d*%d magic matrix\n",N,N);
printf("__________________________________\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%5d",square[i][j]);
printf("\n");
}
printf("__________________________________\n");
}
void magic()
{
int i,j,p,q,key;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
square[i][j]=0;
square[0][(N-1)/2]=1;
i=0;
j=(N-1)/2;
key=2;
while(key<=N*N)
{
p=i-1;
q=j-1;
if(p<0) p=N-1;
if(q<0) q=N-1;
if(square[p][q]!=0)
i=i+1;
else
{
i=p;
j=q;
}
square[i][j]=key;
key++;
}
}
[[it] 本帖最后由 huizi0711 于 2008-6-11 14:41 编辑 [/it]]
[[it] 本帖最后由 huizi0711 于 2008-6-11 14:42 编辑 [/it]]