zoukankan      html  css  js  c++  java
  • 深搜最基础题---全排列And组合数

    这个是理解标记和取消标记,用一个vis数组来标记

    全排列代码:

     1 #include <stdio.h>
     2 
     3 int a[10];
     4 int vis[10];
     5 int n;
     6 void dfs(int step)//step是当前已经进去排列的个数 
     7 {
     8     if (step == n)//如果找到n个之后,打印一次 
     9     {
    10         for (int i = 0; i < n; i++)
    11             printf("%d ", a[i]);
    12         puts("");
    13         return ;
    14     }
    15     for (int i = 1; i <= n; i++)
    16     {
    17         if (vis[i] == 0)//排列中没有此数字,就将它加入到排列中 
    18         {
    19             vis[i] = 1;
    20             a[step] = i;
    21             dfs(step + 1);
    22             vis[i] = 0;
    23         }
    24     }
    25 }
    26 int main()
    27 {
    28     while (scanf("%d", &n) == 1)
    29     {
    30         dfs(0);
    31     }
    32     return 0;
    33 }

    组合数代码(NYOJ-32):

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int n, r;
     5 int a[11];
     6 int vis[11];
     7 void dfs(int step)
     8 {
     9     if (step == r + 1) 
    10     {
    11         for (int i = 1; i <= r; i++)
    12             printf("%d", a[i]);
    13         puts("");
    14         return ;
    15     }
    16     for (int i = n; i > 0; i--)
    17     {
    18         if (vis[i] == 0 && i < a[step - 1])//满足逆序 
    19         {
    20             vis[i] = 1;
    21             a[step] = i;
    22             dfs(step + 1);
    23             vis[i] = 0;
    24         }
    25     }
    26 }
    27 int main()
    28 {
    29     a[0] = 19;
    30     while (scanf("%d %d", &n, &r) == 2)
    31     {
    32         memset(vis, 0, sizeof(vis));
    33         
    34         dfs(1);
    35     } 
    36     
    37     return 0;
    38 }
  • 相关阅读:
    7.3形成团队结构
    第7章 设计构架
    第6章 空中交通管制:高可用性设计案例分析
    5.5安全性战术
    第5章实现质量属性
    4..4.7 使用一般场景进行沟通的概念
    4.4.3性能
    第II部分创建构架
    3.3.2使用结构
    docker容器互联
  • 原文地址:https://www.cnblogs.com/Howe-Young/p/4063391.html
Copyright © 2011-2022 走看看