蓝桥杯训练 历届试题 带分数 (全排列 + 枚举)

问题描述

100 可以表示为带分数的形式:100 = 3 + 69258 / 714。

还可以表示为:100 = 82 + 3546 / 197。

注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。

类似这样的带分数,100 有 11 种表示法。

输入格式

从标准输入读入一个正整数N (N<1000*1000)

输出格式

程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。

注意:不要求输出每个表示,只统计有多少表示法!

样例输入1

100

样例输出1

11

样例输入2

105

样例输出2

6

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;

int num[9]={1,2,3,4,5,6,7,8,9};
int ans=0;

void fun(int n)
{
	int a=0,b=0,c=0;
	int i,j,k;
	for (i=0;i<=6;i++)
	{
		a = a*10 + num[i];
		
		if (a>n) break;
		
		b=0; 
		for (j=i+1;j<=7;j++)
		{
			b = b*10 + num[j];
			c = 0;
			for (k=j+1;k<=8;k++)
			{
				c = c*10 + num[k];
				if (c > b) break;
			}
			if (k>8 && b%c==0 && n==a+b/c)
			ans++; 
		}
	}
	
}
int main()
{
	int n;
	cin>>n;
	ans=0;
	do
	{
		fun(n);
	}while(next_permutation(num,num+9));
	
	cout<<ans<<endl;
	return 0;
}