DFS_全排列

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <vector>
 7 #define sc(x) scanf("%d",&(x))
 8 #define sc2(x,y) scanf("%d%d", &(x), &(y))
 9 #define pn printf("%\n")
10 #define PF(x) printf("%d ",x)
11 #define pf(x) printf("%d\n",x)
12 #define CL(x, y) memset(x, y, sizeof(x))
13 #define FOR(i,b,e)  for(int i = b; i <= e; i++)
14 #define max(a, b) (a > b ? a : b)
15 #define ABS(a, b) (a > b ? a - b : b - a)
16 using namespace std;
17 const int MAX = 25;
18 int ans[MAX], used[MAX], n, N = 0;
19 void show();
20 void DFS(int pos);
21 int main()
22 {
23     sc(n);
24     CL(used, 0);
25     DFS(0);
26     cout << "种类为:" << N << endl;
27     return 0;
28 }
29 void DFS(int pos)
30 {
31     if(pos == n)
32     {
33         show();
34         N++;
35         return ;
36     }
37     FOR(i,1,n)
38     {
39         if(!used[i])
40         {
41             used[i] = 1;
42             ans[pos] = i;
43             DFS(pos+1);
44             used[i] = 0;
45         }
46     }
47 }
48 void show()
49 {
50     FOR(j,0,n-1)
51     PF(ans[j]);
52     cout << endl;
53 }
View Code

 如果直接求种类还是很好办的,到那时其他必须给予数组,或者字符串

string解决

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <vector>
 7 #define sc(x) scanf("%d",&(x))
 8 #define sc2(x,y) scanf("%d%d", &(x), &(y))
 9 #define pn printf("%\n")
10 #define PF(x) printf("%d ",x)
11 #define pf(x) printf("%d\n",x)
12 #define CL(x, y) memset(x, y, sizeof(x))
13 #define FOR(i,b,e)  for(int i = b; i <= e; i++)
14 #define max(a, b) (a > b ? a : b)
15 #define ABS(a, b) (a > b ? a - b : b - a)
16 using namespace std;
17 const int MAX = 25;
18 int n, N = 1, tmp, j;
19 string str;
20 char num[MAX];
21 int main()
22 {
23     sc(n);
24     FOR(i,0,n-1)
25     {
26         sc(tmp);
27         sprintf(num, "%d", tmp);//将数字转化为字符串,可以替代 itoa
28         str.append(num);
29     }
30     sort(str.begin(), str.end());
31     cout << str << endl;
32     while (next_permutation(str.begin(), str.end()))
33     {
34         N++;
35         cout << str << endl;
36     }
37     cout << "种类为:" << N << endl;
38     return 0;
39 }
View Code

char解决

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <vector>
 7 #define sc(x) scanf("%d",&(x))
 8 #define sc2(x,y) scanf("%d%d", &(x), &(y))
 9 #define pn printf("%\n")
10 #define PF(x) printf("%d ",x)
11 #define pf(x) printf("%d\n",x)
12 #define CL(x, y) memset(x, y, sizeof(x))
13 #define FOR(i,b,e)  for(int i = b; i <= e; i++)
14 #define max(a, b) (a > b ? a : b)
15 #define ABS(a, b) (a > b ? a - b : b - a)
16 using namespace std;
17 const int MAX = 25;
18 int n, N = 1, tmp;
19 char ans[MAX], x[MAX];
20 int main()
21 {
22     sc(n);
23     FOR(i,0,n-1)
24     {
25         sc(tmp);
26         sprintf(x, "%d", tmp);
27         ans[i] = *x;//x[0]也可以
28     }
29     sort(ans, ans + n);
30     cout << ans << endl;
31     while(next_permutation(ans, ans+n))
32     {
33         N++;
34         cout << ans << endl;
35     }
36     cout << "种类为:" << N << endl;
37     return 0;
38 }
View Code

 

posted @ 2015-04-10 18:39  PastLIFE  阅读(178)  评论(0编辑  收藏  举报