蓝桥杯 带分数(全排列)

Description

100 可以表示为带分数的形式:100 = 3 + 69258 / 714。
还可以表示为:100 = 82 + 3546 / 197。
注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。
类似这样的带分数,100 有 11 种表示法。

Input

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

Output

程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!

Sample Input 1

100

Sample Output 1

11

Sample Input 2

105

Sample Output 2

6
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <stack>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 const int maxn=1e7+10;
17 using namespace std;
18 
19 int A[10];//存放全排列序列 
20 
21 int main()
22 {
23     int n;
24     scanf("%d",&n);
25     for(int i=0;i<9;i++)//初始化 
26         A[i]=i+1;
27     int ans=0;
28     do{
29         int a=0;//整数部分 
30         for(int i=0;i<=6;i++)//整数部分最多有六位 
31         {
32             a=a*10+A[i];
33             int b=0;//分子部分 
34             int c=0;//分母部分 
35             if(a>=n)
36                 break;
37             for(int j=i+1;j<=i+(9-(i+1)+1)/2;j++)//分子部分最少有剩下位数的一半 
38                 b=b*10+A[j];
39             for(int j=i+(9-(i+1)+1)/2+1;j<9;j++)//剩下位数全给分母部分 
40                 c=c*10+A[j];
41             if(b%c==0&&a+b/c==n)//可知分子/分母必须为整数
42                 ans++;
43             for(int j=i+(9-(i+1)+1)/2+1;j<8;j++)//分子部分变长,分母部分变短 
44             {
45                 b=b*10+A[j];
46                 c=c-A[j]*pow(10,9-(j+1));
47                 if(b%c==0&&a+b/c==n)
48                     ans++;
49             }
50         }
51     }while(next_permutation(A,A+9));
52     printf("%d\n",ans);
53     return 0;
54 }

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11872876.html
今日推荐