• 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("%
    ")
    10 #define PF(x) printf("%d ",x)
    11 #define pf(x) printf("%d
    ",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("%
    ")
    10 #define PF(x) printf("%d ",x)
    11 #define pf(x) printf("%d
    ",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("%
    ")
    10 #define PF(x) printf("%d ",x)
    11 #define pf(x) printf("%d
    ",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
  • 相关阅读:
    个人管理:2,3月微薄整理
    ORM相关图片整理
    LmgORM项目 实体类转换器
    (转)Enterprise Architect 7.0入门教程
    禁止右键复制的代码
    UML示例图(转)学习
    Flash FMS Helloword
    模拟生成Guid
    Flash Media Server安装
    WebSphere创建删除概要文件
  • 原文地址:https://www.cnblogs.com/ghostTao/p/4415433.html
Copyright © 2020-2023  润新知