题目:
寒假作业
现在小学的数学题目也不是那么好玩的。
看看这个寒假作业:
□ + □ = □
□ - □ = □
□ × □ = □
□ ÷ □ = □
每个方块代表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
代码:
#include<stdio.h>
int a[14], book[14],ans=0;
void dfs(int step){
int i;
//判断dfs搜索的边界
if (step == 4)
{
if (!(a[1] + a[2] == a[3]))
return;
}
if (step == 7){
if (!(a[4] - a[5] == a[6]))
return;
}
if (step == 10){
if (!(a[7] * a[8] == a[9]))
return;
}
if (step == 13){
if (a[10] / a[11] == a[12] && a[10] % a[11] == 0){
//可将每一个组合输出出来查看
// printf("%d+%d=%d\n%d-%d=%d\n%d×%d=%d\n%d÷%d=%d\n\n", a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12]);
ans++;
}
return;
}
//尝试每一种可能
for (i = 1; i <= 13; i++){
if (book[i] == 0){
a[step] = i;
book[i] = 1;
//进行下一步
dfs(step + 1);
book[i] = 0;
}
}
return ;
}
int main(){
dfs(1);
printf("\n%d", ans);
return 0;
}