1-9九个数字排成3*3方格,横竖斜要求和为15,怎么写

   阅读
1-9九个数字排成3*3方格,横竖斜要求和为15,如何写
2	7	6
9 5 1
4 3 8
如果是C++的话,可以用那个全排列函数,穷举一遍,因为5在最中间,所以可以减少一点开销。可是java该如何实现呢,貌似没有全排列函数,自己实现的话又有点看不懂百度出来的递归函数,特来请教
------解决思路----------------------
这个。 /
------解决思路----------------------
back-trace, 回溯法实现,这也解决数独类问题最简单直接的算法
------解决思路----------------------
public class MagicSquare {

    public static void main(String[] args) {
        int[][] magicSquare = createMagicSquare( 3 );
        for ( int[] row : magicSquare ) {
            for ( int column : row ) {
                System.out.printf( "%3d" , column );
            }
            System.out.println( );
        }
    }

    public static int[][] createMagicSquare(int rows) {
        rows = rows 
------解决思路----------------------
 1;
        int[][] magicSquare = new int[rows][rows];
        int x = rows / 2, y = 0;
        for ( int i = 1 , k = rows * rows ; i <= k ; i++ ) {
            magicSquare[( y + rows ) % rows][( x + rows ) % rows] = i;
            x = ( x + 1 + rows ) % rows;
            y = ( y - 1 + rows ) % rows;
            if ( magicSquare[y][x] != 0 ) {
                y += 2;
                x--;
            }
        }
        return magicSquare;
    }
}

  8  1  6
  3  5  7
  4  9  2

------解决思路----------------------
不管怎么说,像全排列、数据结构这种基础算法在学习阶段还是不要使用系统提供的为好,你真的会少很多经验值的。
偷偷写了一个你看看吧……
/*
1~9排成3x3表格,横竖斜要求和为15。
*/
class Table {
int d[]={1, 2, 3, 4, 5, 6, 7, 8, 9};

void Write() {
int i;
for(i=0; i<d.length; i++) {
System.out.print(d[i]+" ");
if((i+1)%3==0)
System.out.println();
}
System.out.println();
}

int add(int i, int j, int k) {
return d[i]+d[j]+d[k];
}

boolean check() {
if(add(0, 1, 2)==15 && add(3, 4, 5)==15 && add(6, 7, 8)==15 
&& add(0, 3, 6)==15 && add(1, 4, 7)==15 && add(2, 5, 8)==15
&& add(0, 4, 8)==15 && add(2, 4, 6)==15)
return true;
else
return false;
}

void swap(int i, int j) {
int t;
t=d[i];
d[i]=d[j];
d[j]=t;
}

public void Run(int k) {
int i;
if(k==d.length-1) {
if(check())
Write();
}
else
for(i=k; i<d.length; i++) {
swap(k, i);
Run(k+1);
swap(k, i);
}
}
}

class Exer {
public static void main(String args[]) {
Table t=new Table();
t.Run(0);
}
}
运行结果如下,也不知有错没有。嘿嘿……
2 7 6 
9 5 1 
4 3 8 

2 9 4 
7 5 3 
6 1 8 

4 3 8 
9 5 1 
2 7 6 

4 9 2 
3 5 7 
8 1 6 

6 1 8 
7 5 3 
2 9 4 

6 7 2 
1 5 9 
8 3 4 

8 3 4 
1 5 9 
6 7 2 

8 1 6 
3 5 7 
4 9 2 
------解决思路----------------------
/*
1~9排成3x3表格,横竖斜要求和为15。
*/
class Table {
int d[]={1, 2, 3, 4, 5, 6, 7, 8, 9};

void Write() {
int i;
for(i=0; i<d.length; i++) {
System.out.print(d[i]+" ");
if((i+1)%3==0)
System.out.println();
}
System.out.println();
}

int add(int i, int j, int k) {
return d[i]+d[j]+d[k];
}

boolean check() {
if(add(0, 1, 2)==15 && add(3, 4, 5)==15 && add(6, 7, 8)==15 
&& add(0, 3, 6)==15 && add(1, 4, 7)==15 && add(2, 5, 8)==15
&& add(0, 4, 8)==15 && add(2, 4, 6)==15)
return true;
else
return false;
}

void swap(int i, int j) {
int t;
t=d[i];
d[i]=d[j];
d[j]=t;
}

public void Run(int k) {
int i;
if(k==d.length-1) {
if(check())
Write();
}
else
for(i=k; i<d.length; i++) {
swap(k, i);
Run(k+1);
swap(k, i);
}
}
}

class Exer {
public static void main(String args[]) {
Table t=new Table();
t.Run(0);
}
}

------解决思路----------------------
#include <math.h>  
#include<iostream>
using namespace std;
int a[99][99] = { 0 };
void huanfang(int n)
{
int x = 0, y, b = 1;
y = n / 2;
while (b <= n*n)
{
a[x][y] = b;
int j = x;
int k = y;
j--;
k++;
if (j<0)
j += n;
if (k == n)
k = n - k;
if (0 == a[j][k])
{
x = j;
y = k;
}
else
{
x++;
if (x == n)
x = x - n;
}
b++;
}
}
int main()
{
int n;
cout<<"请输入一个奇数"<<endl;
cin >> n;
huanfang(n);
for (int i = 0; i<n; i++)
{
for (int j = 0; j<n; j++)
cout << a[i][j] << "\t";
cout << endl << endl;
}

return 0; 
}
阅读