思路:
- next_permutation枚举1~9的全排列
- for循环枚举间隔,因为需要3个数,所以需要两个间隔
- 计算x,y,z。判断x+y/z是否等于n
#include<bits/stdc++.h>
using namespace std;
const int N = 20;
typedef long long LL;
int main()
{
int n,cnt;
cin>>n;
cnt = 0;
int a[N]={
0,1,2,3,4,5,6,7,8,9};
do
{
for(int i=1;i<=7;i++)
{
for(int j=i+1;j<=8;j++)
{
LL x,y,z;
x=0;
y=0;
z=0;
int res = 1;
//计算x
for(int k=i;k>=1;k--)
{
x+=a[k]*res;
res*=10;
}
//计算y
res=1;
for(int k=j;k>=i+1;k--)
{
y+=a[k]*res;
res*=10;
}
//计算z
res=1;
for(int k=9;k>=j+1;k--)
{
z+=a[k]*res;
res*=10;
}
if(y%z==0&&n==(x+y/z)) cnt++;
//cout<<x<<" "<<y<<" "<<" "<<z<<endl;
}
}
}while(next_permutation(a+1,a+10));
printf("%d",cnt);
return 0;
}