历届试题 带分数-(全排列)

问题描述

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
解题过程:
全排列暴搜
1.n = x + b/c
2.x<n
3.b%c=0
4.b>c  b的长度大于等于c
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<vector>
 5 #include<stack>
 6 #include<set>
 7 #include<queue>
 8 #include<cstring>
 9 #define ll long long
10 using namespace std;
11 
12 int a[9];
13 
14 int main()
15 {
16     for(int i=0;i<9;i++)
17         a[i]=i+1;///数字1-9
18     int n,x,b,c,ans=0;
19     scanf("%d",&n);
20     do
21     {
22         x=0;
23         for(int i=0;i<6;i++)/// n<= 1000000
24         {
25             x=x*10+a[i];
26             if(x<n)
27             {
28                 int idx=(8-i)/2;///对x后面的数位折半 确保b>c
29                 while((i+idx)<8)
30                 {
31                     b=0;c=0;
32                     int j;
33                     for(j=i+1;j<=(i+idx);j++)
34                         b=b*10+a[j];
35                     for(;j<9;j++)
36                         c=c*10+a[j];
37                     if(b%c==0 && b>c && n==(x+b/c) )
38                         ans++;
39                     idx++;
40                 }
41             }
42         }
43     }while(next_permutation(a,a+9));///对数组a里下标为0到下标为8这九个数全排列
44     printf("%d\n",ans);
45     return 0;
46 }

猜你喜欢

转载自www.cnblogs.com/shoulinniao/p/10434712.html