且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C中所有可能的组合

更新时间:2023-02-18 10:28:22

***有C code代表的 n元灰code 。它应该可以转换成你的问题,通过使用数字作为偏移到您的输入数组。你需要做一些动态分配处理您输入的任意长度。一个相关的办法是做嵌套循环,那就是你有循环计数器阵列只要你输入,另一个柜台为其中那些你正在递增。例如。打印所有六位数基六个号码,需要进行修改的动态分配,但显示的原则:

Wikipedia has C code for the n-ary Gray code. It should be convertible to your problem by using the digits as offsets into your input array. You will need to do some dynamic allocation to handle the arbitrary length of your input. A related approach is to do nested loops, where you have an array of loop counters as long as your input, and another counter for which of those you are currently incrementing. E.g. printing all six-digit base-six numbers, needs to be modified for dynamic allocation but shows the principle:

int i;
int length = 5;
int max = 6;
int counters[length];
for (i=0; i<length; i++)
    counters[i] = 0;
for(;;) {
    for (i=length-1; i>=0; i--)
        printf("%d", counters[i]);
    printf("\n");
    for(i=0; i<length; i++) {
        counters[i]++;
        if (counters[i] < max)
            break;
        else
            counters[i] = 0;
    }
    if (i >= length)
        break;
}