【问题标题】:generate all permutations with repetition....non-recursive in C用重复生成所有排列......在C中非递归
【发布时间】:2015-10-30 04:34:21
【问题描述】:

所以我想知道如何编写一个非递归函数来打印给定 N 和 r 的所有排列,其中r^N 为您提供排列的总数。

Example: N = 3, r = 2, total permutations = 8

output:
000
001
010
011
100
101
110
111

这是我尝试过的,但当然它只适用于一种情况:

void perm_iter(int N, int nr_vals){

    int pos = N-1;
    int i,j,k;
    int P_array[N];
    for(i=0;i<N;i++){
        P_array[i] = 0;
    }
    int val_array[nr_vals];
    for(i=0;i<nr_vals;i++){
        val_array[i] = i;
    }

    do{
        for(i=0;i<N;i++){
            for(j=0;j<nr_vals;j++){
                P_array[pos-1] = val_array[j];
                for(k=0;k<nr_vals;k++){
                    P_array[pos] = val_array[k];
                    for(i=0;i<N;i++){
                        printf("%d",P_array[i]);
                    }
                    printf("\n");
                }
            }
            pos--;
        }
    }while(pos > 0);
}

【问题讨论】:

  • 一个使用队列很容易。
  • 以前从未使用过。我会调查的。谢谢
  • 我相信谷歌不会出现在你的世界这一边。无论如何,请查看-codeproject.com/Articles/138934/…bytes.com/topic/c/answers/885226-all-permutations-non-recursive
  • 第一篇文章对我来说有点粗略,因为我刚刚学习 Java。我可以关注第二个,但我不能使用 Modulus 并且我试图不使用字符串。感谢您的帮助
  • 您需要做的就是数数。 r 是基数,N 是位数。

标签: c permutation non-recursive


【解决方案1】:

这是一个带有可变基数的里程表函数,而不是真正的排列。

#include <stdio.h>
#include <stdlib.h>

void show(int *a, int n)
{
int i;
    for(i = 0; i < n; i++)
        printf("%1d", a[i]);
    printf("\n");
}

void gen_all_numbers(int r, int n)
{
int i;
int *a;
    if(r < 2 || n < 1)          /* parameter check */
        return;
    r -= 1;                     /* r = max digit value */
    a = malloc(n * sizeof(int));
    for(i = 0; i < n; i++)      /* start with all zeroes */
        a[i] = 0;
    show(a, n);
    while(1){
        i = n - 1;
        while(a[i] < r){        /* increment last digit */
            a[i]++;
            show(a,n);
        }
        /* find next digit to increment */
        while(i >= 0 && a[i] == r)
            i--;
        if(i < 0)break;         /* return if done */
        a[i]++;
        while(++i < n)          /* zero following digits */
            a[i] = 0;
        show(a,n);
    }
    free(a);
}

int main()
{
    gen_all_numbers(2,4);
    return 0;
}

【讨论】:

  • 谢谢。它在大多数情况下对我有用,但由于某种原因,如果您输入 N 值为 4,它不喜欢它。它似乎卡在第二个 while 循环,while(a[i] 的无限循环
  • 想通了...它只是不喜欢您的 malloc 我将其更改为 a = malloc(sizeof(int)*(N+1));它工作得很好。再次感谢
  • @JoelMelendez - 我更新了我的答案。 /* find next digit to increase */ 之后的部分正在测试 a[-1] == r,这可能导致访问问题。在测试 a[i] == r 之后,while 循环现在递减 i。我不知道为什么你对 N = 4 有问题,因为这是我测试的。我还添加了一个参数检查以确保 r >= 2,并且 n >= 1。
【解决方案2】:

这行得通。只需确保在编译时使用-lm 选项,否则您将收到有关pow 函数的错误。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void perm(double r,double N){
  double num=pow(r,N);
  int count=(int)num;
  int n=0,b=0;
  for (n=0;n<count;n++){
    printf("%d: ",n);
    for (b=2;b>=0;b--){ //go through bits 2 to 0.
      if (n & (1<<b)){printf("1");}else{printf("0");}
    }
    printf("\n");
  }
}

int main(){
  perm(2,3);
  return 0;
}

【讨论】:

  • 谢谢。我什至没有想过使用比特。太棒了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 2012-11-04
  • 2011-11-22
相关资源
最近更新 更多