枚举数组所有组合

#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;
}

  

posted @ 2021-11-15 20:05  CrossPython  阅读(146)  评论(0编辑  收藏  举报