Coxeter魔方程序

#include <stdio.h>
#define MAX_SIZE 15
void main(void)
/*construct a magic square, iteratively*/
{
        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);
    
/*check for input errors*/
        if(size < 1 || size > MAX_SIZE + 1){
                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; /*middle of first row*/
/*i and j are current position*/
        i = 0;
        j = (size - 1) / 2;
        for(count = 2; count <= size*size; count++){
                row = (i - 1 < 0) ? (size - 1) : (i - 1); /*up*/
                colum = (j - 1 < 0) ? (size - 1) : (j - 1); /*left*/
                if(aquare[row][column]) /*down*/
                        i = (++ i) % size;
                else{
                        i = row;
                        j = (j - 1 < 0) ? (size - 1) : --j;
                }
                square[i][j] = count;
        }
    
/*output the magic square*/
        printf("Magic Square of size %d : \n\n", size);
        for(i = 0; i < size; i++){
            for(j = 0; j < size; j++)
                        printf("%5d", square[i][j]);
            printf("\n");
        }
        printf("\n\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值