一、幻方按照階數可分成了三類,即奇數階幻方、雙偶階幻方、單偶階幻方。
二、奇數階幻方(勞伯法)
奇數階幻方最經典的填法是羅伯法。填寫的方法是:
把1(或最小的數)放在第一行正中;按以下規律排列剩下的(n×n-1)個數:
(1)每一個數放在前一個數的右上一格;
(2)如果這個數所要放的格已經超出了頂行那么就把它放在底行,仍然要放在右一列;
(3)如果這個數所要放的格已經超出了最右列那么就把它放在最左列,仍然要放在上一行;
(4)如果這個數所要放的格已經超出了頂行且超出了最右列,那么就把它放在底行且最左列;
(5)如果這個數所要放的格已經有數填入,那么就把它放在前一個數的下一行同一列的格內。
例,用該填法獲得的5階幻方:
17 |
24 |
1 |
8 |
15 |
23 |
5 |
7 |
14 |
16 |
4 |
6 |
13 |
20 |
22 |
10 |
12 |
19 |
21 |
3 |
11 |
18 |
25 |
2 |
9 |
二、雙偶數階幻方(海爾法)
所謂雙偶階幻方就是當n可以被4整除時的偶階幻方,即4K階幻方。在說解法之前我們先說明一個“互補數”定義:就是在n階幻方中,如果兩個數的和等於幻方中最大的數與1的和(即n×n+1),我們稱它們為一對互補數。如在三階幻方中,每一對和為10的數,是一對互補數 ;在四階幻方中,每一對和為17的數,是一對互補數。
雙偶數階幻方最經典的填法是海爾法。填寫的方法是:
以8階幻方為例:
(1)先把數字按順序填。然后,按4×4把它分割成4塊(如圖)
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
(2)每個小方陣對角線上的數字(如左上角小方陣部分),換成和它互補的數。
64 |
2 |
3 |
61 |
60 |
6 |
7 |
57 |
9 |
55 |
54 |
12 |
13 |
51 |
50 |
16 |
17 |
47 |
46 |
20 |
21 |
43 |
42 |
24 |
40 |
26 |
27 |
37 |
36 |
30 |
31 |
33 |
32 |
34 |
35 |
29 |
28 |
38 |
39 |
25 |
41 |
23 |
22 |
44 |
45 |
19 |
18 |
48 |
49 |
15 |
14 |
52 |
53 |
11 |
10 |
56 |
8 |
58 |
59 |
5 |
4 |
62 |
63 |
1 |
三、單偶數階幻方(斯特拉茲法)
所謂單偶階幻方就是當n不可以被4整除時的偶階幻方,即4K+2階幻方。如(n=6,10,14……)的幻方。
單偶數階幻方最經典的填法是斯特拉茲法。填寫的方法是:
以10階幻方為例。這時,k=2。
(1)把魔方陣分為A,B,C,D四個象限,這樣每一個象限肯定是奇數階。用羅伯法,依次在A象限,D象限,B象限,C象限按奇數階幻方的填法填數。
(2)在A象限的中間行、中間格開始,按自左向右的方向,標出k格。A象限的其它行則標出最左邊的k格。將這些格,和C象限相對位置上的數互換位置。
(3)在B象限所有行的中間格,自右向左,標出k-1格。(注:6階幻方由於k-1=0,所以不用再作B、D象限的數據交換),將這些格,和D象限相對位置上的數互換位置。
四、源代碼如下,已加詳細注釋
- #include<stdio.h>
- #include<stdlib.h>
- int array[15][15];
- int init(int degree) //初始化
- {
- int i;
- int j;
- for(i=0; i<=degree+1; i++)
- for(j=0; j<=degree+1; j++)
- array[i][j] = 0;
- return 0;
- }
- int test_print(int x, int y, int w, int h) //測試用的,輸出以(x,y)為原點,寬為w,高為h,這個區域的數值
- {
- int i;
- int j;
- for(i=y; i<=y+h-1; i++){
- for(j=x; j<=x+w-1; j++){
- printf("%2d ",array[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
- int lao_bo_er(int degree, int x, int y, int num) //勞伯法
- {
- int i;
- int j;
- int k;
- i = y;
- j = degree/2 + x;
- for(k=num; k<=num+degree*degree-1; k++){
- array[i][j] = k;
- if((k-num+1)%degree == 0){ //如果這個數所要放的格已經有數填入
- i = (i-y+1)%degree+y;
- }
- else{ //每一個數放在前一個數的右上一格
- i = (i-y-1+degree)%degree+y;
- j = (j-x+1)%degree+x;
- }
- }
- return 0;
- }
- int seq_range(int degree) //把數字按順序填
- {
- int i;
- int j;
- int num;
- num = 1;
- for(i=1; i<=degree; i++){
- for(j=1; j<=degree; j++){
- array[i][j] = num++;
- }
- }
- return 0;
- }
- int si_te_la_zi(int degree, int x, int y, int num) //斯特拉茲法
- {
- int deg;
- int k;
- int temp;
- int i;
- int j;
- deg = degree/2;
- lao_bo_er(deg, x, y, num); //用羅伯法,依次在A象限,D象限,B象限,C象限按奇數階幻方的填法填數
- lao_bo_er(deg, x+deg, y, num+2*deg*deg);
- lao_bo_er(deg, x, y+deg, num+3*deg*deg);
- lao_bo_er(deg, x+deg, y+deg, num+deg*deg);
- k = (degree-2)/4;
- for(i=1; i<=deg; i++){ //A象限和C象限對換數據
- for(j=1; j<=k; j++){
- temp = array[i][j];
- array[i][j] = array[i+deg][j];
- array[i+deg][j]=temp;
- }
- for(j=deg+deg/2+1; j>=deg+deg/2-k+3; j--){
- temp = array[i][j];
- array[i][j] = array[i+deg][j];
- array[i+deg][j]=temp;
- }
- }
- for(i=j=1; j<=deg/2+k; j++){ //B象限和D象限對換數據
- temp = array[i+deg/2][j];
- array[i+deg/2][j] = array[i+deg+deg/2][j];
- array[i+deg+deg/2][j]=temp;
- }
- return 0;
- }
- int hai_er_fa(int degree) //海爾法
- {
- int i;
- int j;
- int complement;
- int deg;
- seq_range(degree);
- complement = degree*degree+1;
- deg = degree/4;
- for(i=0; i<deg; i++){
- for(j=0; j<deg; j++){ //對角線上的數字換成和它互補的數
- array[i*4+1][j*4+1] = complement - array[i*4+1][j*4+1];
- array[i*4+1][j*4+4] = complement - array[i*4+1][j*4+4];
- array[i*4+4][j*4+1] = complement - array[i*4+4][j*4+1];
- array[i*4+4][j*4+4] = complement - array[i*4+4][j*4+4];
- array[i*4+2][j*4+2] = complement - array[i*4+2][j*4+2];
- array[i*4+2][j*4+3] = complement - array[i*4+2][j*4+3];
- array[i*4+3][j*4+2] = complement - array[i*4+3][j*4+2];
- array[i*4+3][j*4+3] = complement - array[i*4+3][j*4+3];
- }
- }
- return 0;
- }
- int main()
- {
- int degree;
- printf("please input the degree\n");
- scanf("%d",°ree);
- init(degree);
- if(degree%2 == 1){ //奇數階幻方
- lao_bo_er(degree,1,1,1);
- test_print(1,1,degree,degree);
- }
- else if(degree%4 == 2){ //雙偶階幻方
- si_te_la_zi(degree, 1, 1, 1);
- test_print(1,1,degree,degree);
- }
- else{ //單偶階幻方
- hai_er_fa(degree);
- test_print(1,1,degree,degree);
- }
- return 0;
- }