62,614
社区成员
发帖
与我相关
我的任务
分享
2 7 6
9 5 1
4 3 8
如果是C++的话,可以用那个全排列函数,穷举一遍,因为5在最中间,所以可以减少一点开销。可是java该如何实现呢,貌似没有全排列函数,自己实现的话又有点看不懂百度出来的递归函数,特来请教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);
}
}
public class SumIs15 {
private int[] array;
public SumIs15() {
array = new int[] { 1, 2, 3, 4, 6, 7, 8, 9 };
}
public void test() {
Permutation per = new Permutation();
do {
if (array[0] + array[1] + array[2] != 15)
continue;
if (array[3] + array[4] + 5 != 15)
continue;
if (array[5] + array[6] + array[7] != 15)
continue;
if (array[0] + array[3] + array[5] != 15)
continue;
if (array[1] + array[6] + 5 != 15)
continue;
if (array[2] + array[4] + array[7] != 15)
continue;
if (array[0] + array[7] + 5 != 15)
continue;
if (array[2] + array[5] + 5 != 15)
continue;
System.out.printf("%d %d %d\n", array[0], array[1], array[2]);
System.out.printf("%d 5 %d\n", array[3], array[4]);
System.out.printf("%d %d %d\n\n", array[5], array[6], array[7]);
} while (per.nextPermutation(0, array.length));
}
public static void main(String[] args) {
SumIs15 example = new SumIs15();
example.test();
}
private class Permutation {
public boolean nextPermutation(int start, int end) {
if (start == end)
return false;
if (start == end - 1)
return false;
int i = end - 1;
while (true) {
int t = i;
i--;
if (array[i] < array[t]) {
int j = end;
while (!(array[i] < array[--j]))
;
swap(i, j);
reverse(t, end);
return true;
}
if (i == start) {
reverse(start, end);
return false;
}
}
}
private void swap(int x, int y) {
int temp = array[x];
array[x] = array[y];
array[y] = temp;
}
private void reverse(int start, int end) {
for (int i = 0; i < (end - start) / 2; ++i) {
swap(start + i, end - i - 1);
}
}
}
}
大神们来讲讲吧