简单枚举(算法竞赛入门经典)

原题:

输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰为数字0~9的一个排列,2<=n<=79.

AC代码:

 1 #include<iostream>
 2 using namespace std;
 3 bool test(int i,int j);
 4 int main()
 5 {
 6     int n;
 7     cin>>n;
 8     int k=1234;
 9     while(k<=98765)//首先数字要小于98675
10     {
11         int m=k*n;
12         if(m<=98675)
13         {
14            if(test(m,k))
15         {
16             if(k<10000)
17             cout<<m<<'/'<<'0'<<k<<'='<<n<<endl;
18             else
19             cout<<m<<'/'<<k<<'='<<n<<endl;
20 
21         }
22         }
23 
24         k++;
25     }
26 }
27 bool test(int i,int j)
28 {
29     int c[10]={0};
30     int t,s=0;
31     while(i)
32     {
33         t=i%10;
34         i/=10;
35         c[s++]=t;
36     }
37     while(j)
38     {
39         t=j%10;
40         j/=10;
41         c[s++]=t;
42     }
43     for(int d=0;d<10;d++)
44     for(int l=d+1;l<10;l++)
45     if(c[d]==c[l]) return false ;
46     return true;
47 }
原文地址:https://www.cnblogs.com/khbcsu/p/3865384.html