思路:通过DFS全排列9位数,将9位数分为3块,通过i,j双指针实现,第一块为a,第二块为b,第三块为c,如果target = a+b/c,则计数器+1
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 10;
bool st[N];
int num[N],cnt = 0;
int a,b,c,target;
int cal(int l,int r)//将数组中的区间变成实际的数字
{
int res = 0;
for(int i = l;i <= r;i ++)
{
res = res * 10 + num[i];
}
return res;
}
void check()//分成三段,然后判断三段是否符合公式
{
for(int i = 1;i <= 7;i ++)
{
for(int j = i + 1;j <= 8;j ++)
{
a = cal(1,i);
b = cal(i+1,j);
c = cal(j+1,9);
if(target * c == a * c + b) cnt ++;
}
}
}
void dfs(int x)//全排列所有数字
{
if(x > 9)
{
check();
return;
}
for(int i = 1;i <= 9;i ++)
{
if(!st[i])
{
num[x] = i;
st[i] = true;
dfs(x + 1);
st[i] = false;
}
}
}
int main()
{
cin >> target;
dfs(1);
cout << cnt;
return 0;
}