生成所有重复排列…C中的非递归排列
所以我想知道如何编写一个非递归函数来打印给定N和r的所有置换,其中r^N给出置换的总数生成所有重复排列…C中的非递归排列,c,permutation,non-recursive,C,Permutation,Non Recursive,所以我想知道如何编写一个非递归函数来打印给定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]; f
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);
}
这很有效。只需确保在编译时使用-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;
}
这是一个基数可变的里程表函数,不是真正的排列
#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;
}
一次使用队列很容易。以前从未使用过。我会调查的。谢谢,我相信谷歌不会出现在你这一边。不管怎么说,看看吧——第一篇文章对我来说有点粗糙,因为我刚刚在学习Java。我可以跟着第二个,但我不能使用模,我尽量不使用字符串。谢谢你的帮助,尽管你需要做的只是数数。r是基数,N是位数。谢谢。它在大多数情况下对我有效,但出于某种原因,如果你输入N值4,它就不喜欢了。它似乎在第二个while循环中卡住了,而a[i]