奇数阶魔方阵

 
/*
 * Author: Clark <dearvoid@263.net>
 *
 * $Date: 2005/01/24 07:38:34 $
 * $Source: /cvsroot/clark/c/magic.c,v $
 * $Revision: 1.2 $
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int i;
    int n = 0;
    int width;
    int row, col;
    int *array;
    char buf[64];

    if (argc != 2) {
        printf("usage:/n");
        printf("/t%s <an odd number>/n", argv[0]);
        return 1;
    }

    n = atoi(argv[1]);
    if ((n & 1) == 0) {
        printf("error: odd number expected/n");
        return 1;
    }

    array = (int *)malloc(n * n * sizeof(int));
    if (array == NULL) {
        perror("malloc()");
        return 1;
    }

    for (i = 0; i < n * n; i++) {
        array[i] = 0;
    }

    for (i = 1, row = 0, col = n >> 1; i <= n * n; i++) {
        array[row * n + col] = i;
        if (array[(row + n - 1) % n * n + (col + 1) % n]) {
            row = (row + 1) % n;
        } else {
            row = (row + n - 1) % n;
            col = (col + 1) % n;
        }
    }

    sprintf(buf, "%d", n * n);
    width = 1 + strlen(buf);

    for (row = 0; row < n; row++) {
        for (col = 0; col < n; col++) {
            printf("%*d", width, array[row * n + col]);
        }
        printf("/n");
    }
    return 0;
}
=================================
=================================

楊輝魔方陣

楊輝《續古摘奇算經》(1275年)
九子斜排 上下對易 左右相更 四維挺進
戴九履一 左三右七 二四為肩 六八為足
http://residence.educities.edu.tw/mario123/problems/magic-2.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值