奇数阶魔方阵源代码(转)




#include <stdio.h>
#include <stdlib.h>
#define N 5   /*确定魔方阵的大小*/
main()
{
    long a[N][N]={0};
    int count = 1, i = 0, j = (N-1)/2;
   
    while (count <= N * N)
    {
        a[i][j] = count;
        i--;         /*指定下一个数*/
        j++;
        if(i < 0 && j <= N - 1)                        /*i和j范围可能出现的异常情况*/   
            i = N - 1;       
        else if(i >= 0 && j > N - 1)                       
            j = 0;       
        else if(a[i][j] || (i < 0 && j > N - 1) )  
            {i = i + 2; j = j - 1;}
       
        count++;     
    }
   
    for (i = 0; i < N; i++)               /*输出魔方阵*/
    {
        for (j = 0; j < N; j++)
        {
            printf("%8d", a[i][j]);   
        }
        printf(""n"n");
    }
   
    system("pause");     
}
posted @ 2008-11-06 14:01  alex_lin  阅读(301)  评论(0编辑  收藏  举报