小时候看过一道有趣的题,1,2,3,4,5,6,7,8,9进行如下排列,横,竖,斜全是15

来源:百度知道 编辑:UC知道 时间:2024/05/27 18:26:17
4 9 2
3 5 7
8 1 6 类似与上面就行,(因为他们的顺序可以交换)

用编程的方法怎样求得,用C ,C++, JAVA 都可以,要完整的编程噢!

/* 参考
http://baike.baidu.com/view/325330.htm
*/
/*
0 1 2
+------>Y
0 |4 9 2
1 |3 5 7
2 |8 1 6
X
*/

#include <stdio.h>
#define N 3
/* N 必须为奇数 */

void main()
{
int arr[N][N]={0};
int i,j;
int nowx,nowy;
nowx=0;
nowy=N/2;
i=1;
while( i<=N*N )
{
if( arr[nowx][nowy]==0 )
{
arr[nowx][nowy]=i;
nowx=(nowx-1 +N)%N;
nowy=(nowy+1 +N)%N;
i++;
}
else
{
nowx=(nowx-1 + N)%N;
nowy=(nowy-1 +N)%N;
}
}
for( i=0; i<N; i++)
{
for( j=0; j<N; j++)
printf("%4d",arr[i][j]);
puts(&qu