zoukankan      html  css  js  c++  java
  • 递归实现数字的组合(C++)

    递归实现数字的组合

    1. #include <stdio.h>
    2. void Combin(int m,int n,int a)
    3. {
    4.     if(m == n){
    5.         if(a>0)printf("%d",a);
    6.         while(n > 0) printf("%d",n--);
    7.         printf("/n");
    8.         return;
    9.     }
    10.     else if(0 == n){
    11.         printf("%d ",a);
    12.         return;
    13.     }
    14.     Combin(m-1,n-1,a*10+m);
    15.     Combin(m-1,n,a);
    16. }
    17. int main()
    18. {
    19.     Combin(9,5,0);
    20. }

    代码返回:

     

    98765 98764 98763 98762 98761
    98754 98753 98752 98751
    98743 98742 98741
    98732 98731
    98721
    98654 98653 98652 98651
    98643 98642 98641
    98632 98631
    98621
    98543 98542 98541
    98532 98531
    98521
    98432 98431
    98421
    98321
    97654 97653 97652 97651
    97643 97642 97641
    97632 97631
    97621
    97543 97542 97541
    97532 97531
    97521
    97432 97431
    97421
    97321
    96543 96542 96541
    96532 96531
    96521
    96432 96431
    96421
    96321
    95432 95431
    95421
    95321
    94321
    87654 87653 87652 87651
    87643 87642 87641
    87632 87631
    87621
    87543 87542 87541
    87532 87531
    87521
    87432 87431
    87421
    87321
    86543 86542 86541
    86532 86531
    86521
    86432 86431
    86421
    86321
    85432 85431
    85421
    85321
    84321
    76543 76542 76541
    76532 76531
    76521
    76432 76431
    76421
    76321
    75432 75431
    75421
    75321
    74321
    65432 65431
    65421
    65321
    64321
    54321
     
     
  • 相关阅读:
    (转)详谈高端内存和低端内存
    高级声明------定义一个函数指针数组指针
    A Bug's Life POJ
    How Many Answers Are Wrong HDU
    A
    B
    数据处理----离散化
    Serval and Parenthesis Sequence CodeForces
    D
    C
  • 原文地址:https://www.cnblogs.com/fengju/p/6336256.html
Copyright © 2011-2022 走看看