• 递归解决排列组合问题


    /*
    题目: 我们从 1,2,3,4,5,6,7,8,9,10  十个数值中抽取4个数,
    思路:
    1,要从10个数抽取4个数的话 ,我们先抽取一个编号最大的数,10→1 还有9个数,然后从这9个数抽取3个数,同样的道理 
    我们可以从9个数中先抽取编号最大的1个数,然后从这8个数值中抽取2个数,也是同样的道理 ,一直抽到从7个数抽取1个数为止
    
    */
    
    #include<stdio.h>
    #include<conio.h>
    #define M 4
    #define N 10
    
    void  combine(int a[],int b[],int n,int m)
    {
        int i,j;
        for(i=n;i>=m;i--)
        {
            b[m-1] = i-1;
            if(m>1)
                combine(a,b,i-1,m-1);
            else
            {
                for(j=M-1;j>=0;j--)
                    printf("%d  ",a[b[j]]);
                printf("\n");
            }
        }
    }
    
    void main()
    {
        int n =10;
        int a[10] = {1,2,3,4,5,6,7,8,9,10};
        int b[M];
    
        combine(a,b,n,M);
        getch();
    }
  • 相关阅读:
    js的同步与异步
    单体应用SSM
    Spring 事务管理简介
    Linux
    Docker
    spring Cloud Netflix
    平台即服务
    MySQL InnoDB 索引组织表 & 主键作用
    Innodb Double Write
    Laravel 5.6: Specified key was too long error
  • 原文地址:https://www.cnblogs.com/zychengzhiit1/p/2635435.html
Copyright © 2020-2023  润新知