zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    layer常用方法
    使用GLSL实现更多数量的局部光照 【转】
    GLSL实现简单硬件Anisotrop Lighting 【转】
    Bump mapping的GLSL实现 [转]
    图形化OpenGL调试器 BuGLe [转]
    GLSL实现Fresnel And Chromatic aberration 【转】
    GLSL实现Ambient Occlusion 【转】
    GLSL实现Glow效果 [转]
    2013年中国区Skyline软件价格体系
    [OSG]如何用Shader得到物体的世界坐标
  • 原文地址:https://www.cnblogs.com/ghostTao/p/4415433.html
Copyright © 2011-2022 走看看