寒假作业--蓝桥杯

寒假作业

现在小学的数学题目也不是那么好玩的。
看看这个寒假作业:

   □ + □ = □
   □ - □ = □
   □ × □ = □
   □ ÷ □ = □
   
   (如果显示不出来,可以参见【图1.jpg】)
   
每个方块代表1~13中的某一个数字,但不能重复。
比如:
 6  + 7 = 13
 9  - 8 = 1
 3  * 4 = 12
 10 / 2 = 5

以及: 
 7  + 6 = 13
 9  - 8 = 1
 3  * 4 = 12
 10 / 2 = 5

就算两种解法。(加法,乘法交换律后算不同的方案)
 
你一共找到了多少种方案?


请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

64

package com.xjj.lqtest;
/*----寒假作业----
 * 
 * */

public class Win_home {
	public int[] a  = new int[12];			//保存排列数组
	public int[] book = new int[14];		//标记法
	public static int count = 0;			//计数
	
	public void dfs(int n){
		if (n == a.length){					//全部排满
			count++;
			return;
		}

		//枚举每一个数,类似全排列不重复
		for(int i = 1; i <= 13; i++)
			if (book[i] == 0) {
				a[n] = i;					//从0开始保存数
				book[i] = 1;
				if (check(n)) {				//如果满足要求
					dfs(n+1);				//不能用n++
					book[i] = 0;
				}
				book[i] = 0;				//一定要记得在此处也要拿回数
			}
	}
	
	public boolean check(int n){
		//对每个要求进行检查,依次递进
		if (n == 2) 
			if (a[0] + a[1] != a[2]) 
				return false;
		if (n == 5)
			if (a[3] - a[4] != a[5]) 
				return false;
		if (n == 8) 
			if (a[6] * a[7] != a[8]) 
				return false;
		if (n == 11) 
			if (a[9] / a[10] != a[11] || a[9] % a[10] != 0) 
				return false;
		
		return true;

	}

	public static void main(String[] args) {
		Win_home win = new Win_home();
		win.dfs(0);
		System.out.println(count);

	}

}

猜你喜欢

转载自blog.csdn.net/jiejiexiao/article/details/79721491