现在小学的数学题目也不是那么好玩的。
看看这个寒假作业:
□ + □ = □
□ - □ = □
□ × □ = □
□ ÷ □ = □
每个方块代表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
就算两种解法。(加法,乘法交换律后算不同的方案)
你一共找到了多少种方案?
请填写表示方案数目的整数。
看看这个寒假作业:
□ + □ = □
□ - □ = □
□ × □ = □
□ ÷ □ = □
每个方块代表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
就算两种解法。(加法,乘法交换律后算不同的方案)
你一共找到了多少种方案?
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
题目分析:原谅我不厚道的用了stl中的next_permutation函数,全排列,然后判断是否符合情况,符合则++,心疼我电脑跑了几分钟,dfs做法下次有空再写(估计是不写了)【第二天就写好了dfs,发出来,果然比纯暴力枚举快了不是一星半点】。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[15];
int main()
{
for(int i = 1; i<= 13; i++)
a[i] = i;
int counts=0;
do{
if((a[1]+a[2]==a[3]) && (a[4] -a[5] == a[6]) && (a[7]*a[8]==a[9]) && (a[10]==a[11]*a[12]))
counts++;
}
while(next_permutation(a+1,a+14));
cout<<counts<<endl;
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int vis[15];
int a[15];
int ans = 0;
void dfs(int k)
{
if(k == 13)
{
if(a[10] == a[11]*a[12])
ans++;
return ;
}
if(k == 4)
{
if(a[1] + a[2] != a[3])
return;
}
if(k == 7)
{
if(a[4] - a[5] != a[6])
return;
}
if(k == 10)
{
if(a[7] * a[8] != a[9])
return ;
}
for(int i = 1; i <= 13; i++)
{
if(!vis[i])
{
vis[i] = 1;
a[k] = i;
dfs(k+1);
vis[i] = 0;
}
}
}
int main()
{
dfs(1);
cout<<ans<<endl;
return 0;
}