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

乐百川 2015-03-31 01:39:20
2	7	6
9 5 1
4 3 8
如果是C++的话,可以用那个全排列函数,穷举一遍,因为5在最中间,所以可以减少一点开销。可是java该如何实现呢,貌似没有全排列函数,自己实现的话又有点看不懂百度出来的递归函数,特来请教
...全文
2159 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
乐百川 2015-04-02
  • 打赏
  • 举报
回复 1
引用 9 楼 qq_24028085 的回复:
[quote=引用 6 楼 QuickKeyBoard 的回复:]
/*
	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);
	}
}
怎么发帖 带换行的格式啊 [/quote]有个插入代码的按钮,点一下把代码复制进两个标签中间就行了
  • 打赏
  • 举报
回复
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
qq_24028085 2015-04-01
  • 打赏
  • 举报
回复
引用 6 楼 QuickKeyBoard 的回复:
/*
	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);
	}
}
怎么发帖 带换行的格式啊
long小白 2015-04-01
  • 打赏
  • 举报
回复
#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; }
QuickKeyBoard 2015-04-01
  • 打赏
  • 举报
回复
哈哈……这个代码块好玩儿……
QuickKeyBoard 2015-04-01
  • 打赏
  • 举报
回复
/*
	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);
	}
}
QuickKeyBoard 2015-04-01
  • 打赏
  • 举报
回复
不管怎么说,像全排列、数据结构这种基础算法在学习阶段还是不要使用系统提供的为好,你真的会少很多经验值的。 偷偷写了一个你看看吧…… /* 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
乐百川 2015-03-31
  • 打赏
  • 举报
回复
额,憋了一下午我终于弄出了一个,不过nextPermutation函数还是没怎么理解什么意思
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);
			}
		}
	}
}
大神们来讲讲吧
gloomyfish 2015-03-31
  • 打赏
  • 举报
回复
back-trace, 回溯法实现,这也解决数独类问题最简单直接的算法
tony4geek 2015-03-31
  • 打赏
  • 举报
回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧