/* * 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