combinatorics - How to generate all possible variations with repetitions in C -
i'm looking algorithm in c generate possible variations repetitons set lenght , n elements. example if lenght 3 , elements are: 1, 2. output should :
1 1 1
1 1 0
1 0 0
1 0 1
0 0 0
0 0 1
0 1 1
0 1 0
i looked solutions here find implementations in java or python , don't know how rewrite them c. can please post c code problem here?
void g(int l,int v,char *c){int i=v;if(l--==0)puts(c);else while (i)g(l,(c[l]='0'+--i,v),c);}void f(int l,int v){char c[l+2];g((( c[l]=13,c[l+1]=0),l),v,c);}int main(){f(3,2);return 0;} tested, works!, updated fix readability issue
Comments
Post a Comment