how to make permutations?
can anyone understand or get it?Code:if we input 1 1 input 2 1 2 2 1 input 3 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 until we input 5..(so it will print 120, from 5!=5*4*3*2*1)
how to make permutations?
can anyone understand or get it?Code:if we input 1 1 input 2 1 2 2 1 input 3 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 until we input 5..(so it will print 120, from 5!=5*4*3*2*1)
So how would you make a program that prints out permutations like this?
maybe:
put the numbers into an array
sort the array into ascending (low to high) order
You can see the numbers, if they were digits on a car's odometer, just reflect the odometer's wheels, as the car logs in km or miles.
You could use nested for loops for these "wheels".
You could also use a while or do while loop and handled the logic, inside that loop. Perhaps just by a system of swapping them around.
Play with that by hand, and see if it puts any light on the matter.
I don't really get whole what you mean,, but I will try, and help me if I wrong alright.. ^^
You don't see those permutations moving in the same way as the digits on a car's odometer?
hmm..
the last number will add continously until the limit is 3, after that, one before the last, will add one, and if same with the first one, it will change each other(1 to 2, and 2 to 1)?
can I use like that? sorry if my english is not really good..
damn! it won't work.. it will return to the beginning..
If the numbers are consecutive, then they can work like an odometer's "wheels".
If the numbers are not next to each other in value, then you need to use the swap type of logic.
Wikipedia has a big page on permutations, btw.
This is the list of all the permutations for 3 numbers between 1 and 3, in order. Your list did not include all the permutations.
Which gives us:Code:for(n1 = 1; n1 < 4; n1++) { printf("\n"); for(n2 = 1; n2 < 4; n2++) { for(n3 = 1; n3 < 4; n3++) { printf("%d%d%d ", n1, n2, n3); } } }
111 112 113 121 122 123 131 132 133
211 212 213 221 222 223 231 232 233
311 312 313 321 322 323 331 332 333
press enter when ready
When it's put into a program and run.
Were you thinking of just all the combinations, instead?
Last edited by Adak; 10-31-2009 at 03:45 PM.
waw.... after that, we have just only use "\b" or throw away that have the same number right?
by the way,, how it would be, if we must use recursive function?
If you want the combinations, you don't want to generate all the permutations to get them.
Generating all combinations is much quicker than generating all the permutations.
can we use recursif function to print the permutations?
I have already finished it, by using char.. but I still cann't get it,, how the recursift function work.. T_TCode:#include<stdio.h> #include<string.h> #include<ctype.h> int a; char huruf[6]; int permutasi(char set[6],int mulai,int selesai) { int jarak,i; char sementara; jarak=selesai-mulai; if(jarak==1) { printf(" %s\n",set); } else { for(i=0;i<jarak;i++) { sementara=set[mulai+i]; set[mulai+i]=set[mulai]; set[mulai]=sementara; permutasi(set,mulai+1,selesai); sementara=set[mulai+i]; set[mulai+i]=set[mulai]; set[mulai]=sementara; } } return 0; } int main() { int a; do{do{a=0; printf("masukkan n(1-5) : "); scanf("%d",&a);fflush(stdin); }while(!(a>0)); }while(a>1&&a>5); switch(a) { case 1: strcpy(huruf,"1"); break; case 2: strcpy(huruf,"12"); break; case 3: strcpy(huruf,"123"); break; case 4: strcpy(huruf,"1234"); break; case 5: strcpy(huruf,"12345"); break; } permutasi(huruf,0,strlen(huruf)); getchar(); }