C中打印魔方阵

0 views
Skip to first unread message

bruc...@gmail.com

unread,
Mar 22, 2006, 4:30:40 AM3/22/06
to 星星爱CPP
// 编译测试通过 by gcc3.4.2
// 编译参数中加 -std=c99
#include <stdio.h>
#define N 3
int main()
{
int tmp[N][N] = { 0 };
for( int n=1,i=0,j=1; n<=N*N; ++n )
{
if( tmp[i][j] )
{
i = (i+2)%N;
j = (j+N-1)%N;
}
tmp[i][j] = n;
i = (i+N-1)%N;
j = (j+1)%N;
}
for( int i=0; i<N; ++i )
{
for( int j=0; j<N; ++j )
printf( "%2d ", tmp[i][j] );
printf( "\n" );
}

return 0;
}

输出:
8 1 6
3 5 7
4 9 2

Reply all
Reply to author
Forward
0 new messages