/**
* @(#)magicn.java
* @author:Arthur
* @note:魔術方塊(邊長n)
* @version 1.00 2007/10/4
*/
import java.io.*;
public class magicn {
public static void main(String args[])throws IOException {
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("請輸入魔術方塊邊長:");
int n=Integer.parseInt(buf.readLine());
if (n%2==0) System.out.println("邊長必須為奇數!");
else{
int num[][]=new int[n][n];
int x=n-1,y=(n-1)/2,z=1;
while (true){
if (num[x][y]==0) num[x][y]=z;
x++;
y++;
if (x==n) x=0;
if (y==n) y=0;
if (num[x][y]!=0){
x=x-2;
y--;
if (y==-1) y=n-1;
if (x==-1) x=n-1;
if (x==-2) x=n-2;
}
z++;
if (z>=n*n+1) break;
}
for (int i=0;i
}
}
}
}
--------------------Configuration:
請輸入魔術方塊邊長:11
56 69 82 95 108 121 2 15 28 41 54
55 57 70 83 96 109 111 3 16 29 42
43 45 58 71 84 97 110 112 4 17 30
31 44 46 59 72 85 98 100 113 5 18
19 32 34 47 60 73 86 99 101 114 6
7 20 33 35 48 61 74 87 89 102 115
116 8 21 23 36 49 62 75 88 90 103
104 117 9 22 24 37 50 63 76 78 91
92 105 118 10 12 25 38 51 64 77 79
80 93 106 119 11 13 26 39 52 65 67
68 81 94 107 120 1 14 27 40 53 66
Process completed.