算法参考http://baike.baidu.com/view/325330.htm
1.首先用horse法生成奇阶幻方
/** *//**
* horse法生成奇阶幻方
* @author wangliang
*
*/
public class Hf2n_1 {
public static void main(String args[]){
int number=5;
int matrix[][]=createnumber(number);
for(int i=0;i<number;i++){
for(int j=0;j<number;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
public static int[][] createnumber(int n){
int magicmatrix[][]=new int[n][n];
int i=0;
int j=n/2;
for(int m=1;m<=n*n;m++){
magicmatrix[i][j]=m;
int p=(i-1+n)%n;
int q=(j+1+n)%n;
if(magicmatrix[p][q]==0){
i=p;
j=q;
}
else i=(i+1)%n;
}
return magicmatrix;
}
}
2.Strachey法生产4m+2阶的幻方
/** *//**
* Strachey法生产4m+2阶的幻方
* @author wangliang
*
*/
public class Hf4m_2 {
public static void main(String args[]){
int number=10;
int matrix[][]=createnumber(number);
for(int i=0;i<number;i++){
for(int j=0;j<number;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
public static int[][] createnumber(int n){
int a[][]=new int[n][n];
int temp=n/2;
int temp2=(n-2)/4;
int b[][]=Hf2n_1.createnumber(temp);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i<temp&&j<temp){
a[i][j]=b[i][j];
}
else if(i>=temp&&j>=temp){
a[i][j]=b[i%temp][j%temp]+temp*temp;
}
else if(i<temp&&j>=temp){
a[i][j]=b[i%temp][j%temp]+2*temp*temp;
}
else{
a[i][j]=b[i%temp][j%temp]+3*temp*temp;
}
}
}
for(int i=0;i<temp;i++){
if(i!=temp2){
for(int j=0;j<temp2;j++){
int tem=a[i][j];
a[i][j]=a[i+temp][j];
a[i+temp][j]=tem;
}
}
if(i==temp2){
for(int j=1;j<temp2+1;j++){
int tem=a[i][j];
a[i][j]=a[i+temp][j];
a[i+temp][j]=tem;
}
}
for(int j=n-1;j>=n-(temp2-1);j--){
int tem=a[i][j];
a[i][j]=a[i+temp][j];
a[i+temp][j]=tem;
}
}
return a;
}
}
3.Spring法生产4m阶幻方
/** *//**
* Spring法生产4m阶幻方
* @author wangliang
*
*/
public class Hf4m {
public static void main(String args[]){
int number=4;
int matrix[][]=createnumber(number);
for(int i=0;i<number;i++){
for(int j=0;j<number;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
public static int[][] createnumber(int n){
int a[][]=new int[n][n];
for(int i=0;i<n*n;i++){
int p=i/n;
int q=i%n;
a[p][q]=i+1;
}
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if((i+j)%4!=3&&(i-j)%4!=0){
int t1=a[i][j];
a[i][j]=a[n-1-i][n-1-j];
a[n-1-i][n-1-j]=t1;
}
}
}
return a;
}
}
4.生成用户界面
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class MagicMatrix extends JFrame implements ActionListener{
private JPanel center;
private JPanel right;
private JLabel label;
private JSpinner level;
private JButton Louberel;
public MagicMatrix(){
center=createpanel(3);
add(center,BorderLayout.CENTER);
right=new JPanel();
label=new JLabel("选择矩阵阶数");
level=new JSpinner(new SpinnerNumberModel(3,3,100,1));
Louberel=new JButton("生成");
Louberel.addActionListener(this);
right.add(label,BorderLayout.WEST);
right.add(level,BorderLayout.CENTER);
right.add(Louberel,BorderLayout.EAST);
add(right,BorderLayout.NORTH);
setTitle("magic matrix(www.blogjava.net/littlefermat)");
setSize(340,296);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==Louberel){
int levelnumber=((Integer)level.getValue()).intValue();
center=createpanel(levelnumber);
add(center,BorderLayout.CENTER);
validate();
}
}
/** *//**生成相应的面板*/
private JPanel createpanel(int n){
int matrix[][];//生产的矩阵
int matrixshow[][]=new int[n+1][n+1];//现实的矩阵,包括每行,每列和对角线的和
if(n%2==1){
matrix=Hf2n_1.createnumber(n);
}
else if(n%4==0){
matrix=Hf4m.createnumber(n);
}
else {
matrix=Hf4m_2.createnumber(n);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
matrixshow[i][j]=matrix[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
matrixshow[i][n]+=matrix[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
matrixshow[n][j]+=matrix[j][i];
}
}
for(int i=0;i<n;i++){
matrixshow[n][n]+=matrix[i][i];
}
JPanel tempJPanel=new JPanel();
tempJPanel.setLayout(new GridLayout(n+2,n+1));
JLabel jlabel[][]=new JLabel[n+2][n+1];
for(int i=0;i<n+2;i++){
for(int j=0;j<n+1;j++){
if(i==0){
jlabel[i][j]=new JLabel(""+(j+1),JLabel.CENTER);
}
else jlabel[i][j]=new JLabel(""+matrixshow[i-1][j],JLabel.CENTER);
jlabel[i][j].setBorder(BorderFactory.createLineBorder(Color.green));
if(i==n+1||j==n){
jlabel[i][j].setBackground(Color.pink);
jlabel[i][j].setOpaque(true);
}
if(i==0&&j!=n){
jlabel[i][j].setBackground(Color.cyan);
jlabel[i][j].setOpaque(true);
}
tempJPanel.add(jlabel[i][j]);
}
}
return tempJPanel;
}
public static void main(String args[]){
MagicMatrix application=new MagicMatrix();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/Files/littlefermat/src.rar