魔方问题

一个魔方(Magic Square)是由一个1到n^2的整数构成的n*n矩阵,其中每行、每列以及两个对角线上的数字之和相等。如下面是n=5的魔方,行、列、对角线的和分别是75:
          
15812417
16147523
22201364
321191210
92251811
 
n为奇数时,H.Coxeter 给出了魔方生成的简单法则:
    在开始前,先搞清楚三个位置:一个是当前位置(当前已经填入数字的位置,用i, j代表行,列),一个是预计位置(估计下一个数会在哪里,用row, col代表行,列),另一个是下一个位置(下一个数的确切位置,用i, j代表行,列)。因为预计位置不一定就是下一个位置,所以这两个位置有区别的。步骤如下:
    1. 先在第一行中间的位置填入1,然后再找下一个数字(此时是2)的预计位置(估计下一个数会在哪里);
    2. 如果当前位置在第一行上,就让预计位置的行在最后一行(i=max),否则就在上一行(i-1);
    3. 如果当前位置在第一列上,就让预计位置的列在最后一列(j=max),否则就在左一列(j-1);
    4. 如果预计位置还没填入数字,下一个位置就是预计位置;
    5. 如果预计的位置已填入数字,下一个位置就是当前
 
代码如下:

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 15     // 魔方行、列不超过15

int main(void)
{
    static int square[MAX_SIZE][MAX_SIZE];
    int i, j, row, column;
    int count;
    int size;

    printf("Enter the size of the square: ");
    scanf("%d", &size);

    if(size < 1 || size > MAX_SIZE + 1)    // 如果输入的值超出魔方范围
    {
        fprintf(stderr, "Error! Size is out of range\n");
        exit(1);
    }

    if (!(size % 2))      // 如果输入的是偶数
    {
        fprintf(stderr, "Error! Size is even\n");
        exit(1);
    }


    for (i = 0; i < size; i++)
      for (j = 0; j < size; j++)
        square[i][j] = 0;        // 初始化输入的魔方数

    square[0][(size-1) / 2] = 1;  // 在第一行的中间位置放1

    i = 0;                        // 当前位置的行
    j = (size-1) / 2;             // 当前位置的列

    for (count = 2; count <= size * size; count++)
    {
        row = (i-1 < 0) ? (size-1): (i-1);     // 在第一行预计位置就在最后一行,否则就在上一行
        column = (j-1 < 0) ? (size-1): (j-1);  // 在第一列预计位置就在最后一列,否则就在左一列
        if(square[row][column])                // 预计位置已经填入数字下个位置的行就是当前的行
          i = (++i) % size;                    
        else                                   // 预计的位置未填入数字,下一个就是预计位置
        {
            i = row;
            j = (j-1 < 0) ? (size-1): --j;     // j = column
        }
        square[i][j] = count;                  // 填入数字
    }

    printf("Magic Square of size %d: \n", size);
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
          printf("%5d", square[i][j]);

        printf("\n");
    }

    printf("\n");
}

以上问题参考《Fundamentals of Data Structures in C》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include #include #include int a[3][3]={1,1,1,1,1,1,1,1,1}; /*INITIALIZE SIX PLANS*/ int b[3][3]={2,2,2,2,2,2,2,2,2}; int c[3][3]={4,4,4,4,4,4,4,4,4}; int d[3][3]={3,3,3,3,3,3,3,3,3}; int e[3][3]={5,5,5,5,5,5,5,5,5}; int f[3][3]={7,7,7,7,7,7,7,7,7}; int top[3][3],front[3][3],side[3][3]; int position=0,temp[3]={0,0,0},new[3][3]; /*POSITION ASSIGN THE ARROW POSITION*/ int sign; main() { int l=60,x=50,y=50,key,count,input=0,errorcode; /*L MEANS THE LENTH OF THE SQUARE,X MEANS THE INITAIL POSITION OF X,Y MEANS THE INITIAL POSITION OF Y*/ int a1[3][3]={1,1,1,1,1,1,1,1,1}; /*INITIALIZE SIX PLANS FOR BACKUP*/ int b1[3][3]={2,2,2,2,2,2,2,2,2}; int c1[3][3]={4,4,4,4,4,4,4,4,4}; int d1[3][3]={3,3,3,3,3,3,3,3,3}; int e1[3][3]={5,5,5,5,5,5,5,5,5}; int f1[3][3]={7,7,7,7,7,7,7,7,7}; int graphdriver=VGA,graphmode=VGAHI; initgraph(&graphdriver,&graphmode,""); errorcode = graphresult(); if (errorcode != grOk) /*MEET ERROR*/ { /*SHOW THE ERROE*/ printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to exit:"); getch(); exit(1); } drawing(x,y,l); /*DRAWING THE BIG SQUARE*/ drawing(x+400,y,l/2); /*DRAWING THE SMALL SQUARE*/ do { sign=position%3; /*COPY DATA TO SHOW*/ copy (&a,&top); copy (&b,&front); copy (&c,&side); color(x,y,l); /*SHOWING THE BIG SQUARE*/ copy (&e,&top); change2(&top,1); copy (&f,&front); change2(&front,2); copy (&d,&side); color(x+400,y,l/2); /*SHOWING THE SMALL SQUARE*/ setcolor(14); arrow(x,y,l); key=bioskey(0); /*READ KEYBORD*/ switch(key) { case 283:printf("Esc"); /*IF INPUT_KEY="ESC" TO EXIT THE PROGRAM*/ goto end; case 20480:setcolor(0); /*IF INPUT_KEY="DOWN" TO CHANGE THE ARROW*/ arrow(x,y,l); if(position==8) position=0; else position=position+1; break; /*IF INPUT_KEY="UP" TO CHANGE THE ARROW*/ case 18432:setcolor(0); arrow(x,y,l); if(position==0) position=8; else position=position-1;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值