【问题标题】:algorithm for generating number combinations without repetition生成无重复数字组合的算法
【发布时间】:2013-04-20 21:55:26
【问题描述】:

我在这里查看了几乎所有类似的帖子,但我不知道如何做我想做的事。我正在尝试在 C 程序中输入一个输入,比如说数字 4,然后程序在数组中返回以下数字:

1
2
3
4
12
13
14
23
24
34
123
134
124
1234

为了更清楚: 如果输入数字是 4,那么我想使用数字 1-4 并生成所有可能的数字组合(从 1 位组合到 4 位组合),而不需要数字重复。

我尝试了以下代码:

#include <stdio.h>

/* Prints out a combination like {1, 2} */
void printc(int comb[], int k) {
    printf("{");
    int i;
    for (i = 0; i < k; ++i)
        printf("%d, ", comb[i] + 1);
    printf("\\b\\b}\\n");
}


    int next_comb(int comb[], int k, int n) {
    int i = k - 1;
    ++comb[i];
    while ((i >= 0) && (comb[i] >= n - k + 1 + i)) {
        --i;
        ++comb[i];
    }

    if (comb[0] > n - k) /* Combination (n-k, n-k+1, ..., n) reached */
        return 0; /* No more combinations can be generated */

    /* comb now looks like (..., x, n, n, n, ..., n).
    Turn it into (..., x, x + 1, x + 2, ...) */
    for (i = i + 1; i < k; ++i)
        comb[i] = comb[i - 1] + 1;

    return 1;
}

int main(int argc, char *argv[]) {
    int n = 5; /* The size of the set; for {1, 2, 3, 4} it's 4 */
    int k = 3; /* The size of the subsets; for {1, 2}, {1, 3}, ... it's 2 */
    int comb[16]; /* comb[i] is the index of the i-th element in the
            combination */

    /* Setup comb for the initial combination */
    int i;
    for (i = 0; i < k; ++i)
        comb[i] = i;

    /* Print the first combination */
    printc(comb, k);

    /* Generate and print all the other combinations */
    while (next_comb(comb, k, n))
        printc(comb, k);

    return 0;
}

上面的程序打印结果。我想以某种方式得到结果。但我不能,因为上面的代码以一种奇怪的方式打印结果。

【问题讨论】:

  • 你的问题是……?
  • 您试图显示原始输入的某种排列?你的问题不是很清楚。
  • 我修复了输出并添加了更好的解释
  • 啊,所以您正试图修改您在compprog.wordpress.com/2007/10/17/generating-combinations-1 找到的程序,将其输出放入数组而不是标准输出?
  • 没错。那是因为我尝试了我能想到的所有其他方法..

标签: c algorithm combinations permutation


【解决方案1】:

我们使用一个 int 来表示一个集合。对于第 i 位,如果为 1,则 i 在集合中,反之亦然。

举个例子:1010(2)={4,2} 1111(2)={4,3,2,1}

对于将要考虑的每个元素,有两个选择:在集合中或不在集合中。

所以,总共有 2^n 个不同的集合。在我的代码中,我只是枚举了每个可能的 int 对应的集合,并输出对应的集合。

所以我们得到了这个代码:

for(int i=1;i<(1<<n);i++)
{
    for(int j=0;j<n;j++)
        if ((1<<j)&i) printf("%d",j+1);
    puts("");
}

当 n=4 时,输出:

1
2
12
3
13
23
123
4
14
24
124
34
134
234
1234

如果你想按照给出的顺序输出答案,只需将它们变成字符串并将这些字符串放入向量中并排序。

如果 n 很大,可以使用 bitset。但是当n>30时,它可能不会在几小时内终止。所以 int 是有效的。

【讨论】:

  • 为了解决 OP 遇到的具体问题,这太棒了。老实说,我不确定它是标准便携的(我认为是,但需要阅读一下)。如果是这样,它将适用于高达(sizeof(int)*8)-2 的数字,如果您使用unsigned int,您可以挤出多余的部分。如果需要更大的大小,很容易合并一个带有额外循环的字节数组。总而言之,一个很好的答案。
  • 我认为在顶部添加一个描述为什么算法有效会很好。该算法生成(无序)集合并用一组位表示它们。所以{1, 2, 3, 4} = 1111{} = 0000{1, 3} = 1010 等等。它遍历所有位集并通过检查该位是否为“on”(即== 1)生成相应的数字集,如果是则打印它案子。啊,我刚刚注意到它不会打印空集,但这可能是可取的,也可能不是可取的。
  • 啊,我搞砸了订购。更像是{4, 3, 2, 1} = 1111(而不是{1, 2, 3, 4} = 1111
  • 很棒的简短解决方案,有没有办法改变它,使其按顺序生成组合?如1,2,3,4,12,13,23,14,24,34,123,124,134,234,1234
【解决方案2】:

这是一个生成数字组合的程序。它是用 C 编写的。 但它可以用任何其他语言重写。现在,只需编译它并尝试一下!

#include <stdio.h>
#include <stdlib.h>
int v[100], stack[100];
int sp,i,n,g;
int main()
{
printf("Dimension of V:");
scanf( "%d",&n);
//Input numbers
for (i=0 ; i<n ; i++) {
printf("V[%d]=",i);
scanf( "%d",&g);
v[i]=g;
}
printf("running...\n");
sp=-1;
while(1) {
// stack ones until stack overflow
    while(sp<n-1)  {
      sp=sp+1;
      stack[sp]=1;
      for (i=0 ; i<=sp ; i++) if (stack[i]==1) printf("v[%d]=%d ",i,v[i]);
      printf("\n");
   }
// unstack all the zeros until top of stack is equal one
while (stack[sp]==0 && sp>=0) sp=sp-1;
// if Bottom of stack is reached then stop
if (sp<0) break;
// set top of stack from one to zero
      stack[sp]=0;
  }
return 0;
}

运行 n=4 :

[oldache@localhost fin]$ ./comb
Dimension of V:4
V[0]=10
V[1]=20
V[2]=30
V[3]=40
running...
v[0]=10
v[0]=10 v[1]=20
v[0]=10 v[1]=20 v[2]=30
v[0]=10 v[1]=20 v[2]=30 v[3]=40
v[0]=10 v[1]=20 v[3]=40
v[0]=10 v[2]=30
v[0]=10 v[2]=30 v[3]=40
v[0]=10 v[3]=40
v[1]=20
v[1]=20 v[2]=30
v[1]=20 v[2]=30 v[3]=40
v[1]=20 v[3]=40
v[2]=30
v[2]=30 v[3]=40
v[3]=40

【讨论】:

  • 我发现你没有缩进...令人不安。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多