枚举数组所有组合

#include <stdio.h>
#include <stdbool.h>

void PrintAll(int a[], int nBegin, int nEnd, int nCount, char* szPre){
    if(nCount==1){
        for(int i=nBegin; i<=nEnd; i++){
            printf("%s%d ", szPre, a[i] );
        }
        printf("\n");
    }else{
        for(int i=nBegin; i<=nEnd-nCount+1; i++){
            char szBuf[10]= {0};
            sprintf(szBuf, "%s%d", szPre,a[i] );
            PrintAll(a, i+1, nEnd, nCount-1, szBuf);
        }
    }
}

int main(){
    int a[] = {1,2,3,4,5,6};
    PrintAll(a, 0, 5, 3, "");
    return 1;
}

  

原文地址:https://www.cnblogs.com/pythonClub/p/15558028.html