标题:请指教!!关于魔方阵
只看楼主
oscar19850808
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-6-22
 问题点数:0 回复次数:1 
请指教!!关于魔方阵
请指教!!关于魔方阵数据结构问题
#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;
}
以上程序的数据结构怎样表达??????麻烦各位高手点明小弟....清楚详细点,因为我是刚学,新手哦
搜索更多相关主题的帖子: 魔方 指教 
2008-06-24 20:50
爱喝牛奶的猫咪
Rank: 1
来 自:QQ群46520219
等 级:禁止访问
帖 子:513
专家分:0
注 册:2008-6-16
得分:0 
奇数阶幻方生成干嘛写这么长的代码呢??


[color=white]<>
2008-06-24 20:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-221038-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.579462 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved