现在小学的数学题目也不是那么好玩的。
看看这个寒假作业:
每个方块代表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
就算两种解法。(加法,乘法交换律后算不同的方案)
你一共找到了多少种方案?
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字
#include <iostream>
using namespace std;
const int N = 20;
int a[] = { 1,2,3,4,5,6 ,7,8,9,10,11,12,13};
int ans = 0;
bool bt[N];
int b[N];
void dfs(int s, int t)
{
if (s == 12)
{
if (b[9] * b[10] == b[11])
{
ans++;
}
return;
}
if (s == 3 && b[0] + b[1] != b[2])return;
if (s == 6 && b[3] - b[4] != b[5])return;
if (s == 9 && b[6] * b[7] != b[8])return;
for (int i = 0; i < t; i++)
{
if (!bt[i])
{
bt[i] = true;
b[s] = a[i];
dfs(s + 1, t);
bt[i] = false;
}
}
}
void slove()
{
int n=13;
dfs(0, 13);
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio;
cin.tie(0);
cout.tie(0);
int num = 1;
while (num--)
{
slove();
}
}