先枚举前面的整数,然后剩下的数字集中,dfs来枚举排列,枚举分子长度。
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<ctype.h>
#include<set>
#include<math.h>
using namespace std;
bool isUsed[15];
int num[15];
int ans;
int a, n;
int leftCount;
int pow10[12];
bool judge(int fenzilen)
{
int fenzi = 0, fenmu = 0;
for (int i = 0; i<fenzilen; i++)
{
fenzi += num[i] * pow10[10, fenzilen - 1 - i];
}
for (int i = fenzilen; i<leftCount; i++)
{
fenmu += num[i] * pow10[10, leftCount - 1 - i];
}
if (fenzi%fenmu == 0 && a + fenzi / fenmu == n)
{
//printf("%d %d %d\n", a, fenzi, fenmu);
return true;
}
return false;
}
void dfs(int pos)
{
if (pos == leftCount)
{
for (int i = leftCount / 2.0 + 0.5; i<leftCount; i++)
{
if (judge(i))
{
ans++;
}
}
}
for (int i = 1; i <= 9; i++)
{
if (isUsed[i])
continue;
isUsed[i] = true;
num[pos] = i;
dfs(pos + 1);
isUsed[i] = false;
}
}
int main()
{
pow10[0] = 1;
for (int i = 1; i<10; i++)
{
pow10[i] = pow10[i - 1] * 10;
}
scanf("%d", &n);
ans = 0;
for (a = 1; a<n; a++)
{
leftCount = 9;
memset(isUsed, 0, sizeof(isUsed));
int temp = a;
while (temp != 0)
{
if (isUsed[temp % 10]|| temp % 10==0)
break;
isUsed[temp % 10] = true;
leftCount--;
temp /= 10;
}
if (temp != 0)
continue;
dfs(0);
}
printf("%d\n", ans);
}