当前位置: 代码迷 >> C语言 >> 请指教!!关于魔方阵
  详细解决方案

请指教!!关于魔方阵

热度:414   发布时间:2008-06-24 20:50:56.0
请指教!!关于魔方阵
请指教!!关于魔方阵数据结构问题
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 500    /*这里可以修改最大阶*/
int main()
{
    int rows = 0, center = 0, iArray[MAX_NUM][MAX_NUM];
    int RowSet = 0, LineSet = 0, newRowSet = 0, newLineSet = 0;
    int i = 0, j = 0;
    int okNum = 0;
   
    // set the items of array "iArray" to be 0
    for ( i = 0; i < MAX_NUM; i++ )
        for ( j = 0; j < MAX_NUM; j++ )
            iArray[i][j] = 0;
   
    // get the rows number
    while ( 1 )
    {
        printf("输入行数:\n");
        scanf("%d", &rows);
        if ( rows <= MAX_NUM )
        {
                rows -= 1;
                break;
        }
        else {
             printf("行数必须在 0 和 %d 之间, 请重新", MAX_NUM);
        }
    }   
   
    // set number '1'
    center = rows / 2;
    iArray[0][center] = 1;
   
    // initialize the okNum, RowSet and LineSet
    okNum = 1;
    RowSet = 0;
    LineSet = center;
   
    // set each item in "iArray"
    while ( okNum < (rows + 1) * (rows + 1) )
    {
          if ( RowSet == 0 && LineSet == rows )
          {
               RowSet += 1;
          }
          else {
                    newRowSet = (RowSet == 0) ? rows : RowSet - 1;
                    newLineSet = (LineSet == rows) ? 0 : LineSet + 1;         
         
                   if ( iArray[newRowSet][newLineSet] != 0 )
                   // there is already a number here!
                   {
                            RowSet = (RowSet == rows) ? 0 : RowSet + 1;
                            //RowSet += 1;
                   }
                   else{
                            RowSet = newRowSet;
                            LineSet = newLineSet;
                   }
          }
         
          iArray[RowSet][LineSet] = ++okNum;
    }
   
    // print the "iArray"
    for ( i = 0; i <= rows; i++ )
    {
        for ( j = 0; j <= rows; j++ )
            printf("%5d", iArray[i][j]);
        printf("\n");
    }
        
    system("pause");
    return 0;
}
以上程序的数据结构怎样表达??????麻烦各位高手点明小弟....清楚详细点,因为我是刚学,新手哦
搜索更多相关的解决方案: 魔方  指教  

----------------解决方案--------------------------------------------------------
奇数阶幻方生成干嘛写这么长的代码呢??


[color=white]<>
----------------解决方案--------------------------------------------------------
  相关解决方案