![]() |
| | #1 |
| Registered User Join Date: Oct 2009 Location: Indonesia
Posts: 62
| permutations 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) |
| Kinshara is offline | |
| | #2 |
| Registered User Join Date: Sep 2006
Posts: 3,139
| 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. |
| Adak is offline | |
| | #3 |
| Registered User Join Date: Oct 2009 Location: Indonesia
Posts: 62
| I don't really get whole what you mean,, but I will try, and help me if I wrong alright.. ^^ |
| Kinshara is offline | |
| | #4 |
| Registered User Join Date: Sep 2006
Posts: 3,139
| You don't see those permutations moving in the same way as the digits on a car's odometer? |
| Adak is offline | |
| | #5 |
| Registered User Join Date: Oct 2009 Location: Indonesia
Posts: 62
| 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.. |
| Kinshara is offline | |
| | #6 |
| Registered User Join Date: Oct 2009 Location: Indonesia
Posts: 62
| damn! it won't work.. it will return to the beginning.. |
| Kinshara is offline | |
| | #7 |
| Registered User Join Date: Sep 2006
Posts: 3,139
| 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. 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. |
| Adak is offline | |
| | #8 |
| Registered User Join Date: Oct 2009 Location: Indonesia
Posts: 62
| 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? |
| Kinshara is offline | |
| | #9 |
| Registered User Join Date: Sep 2006
Posts: 3,139
| 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. |
| Adak is offline | |
| | #10 |
| Registered User Join Date: Oct 2009 Location: Indonesia
Posts: 62
| can we use recursif function to print the permutations? |
| Kinshara is offline | |
| | #11 |
| Registered User Join Date: Oct 2009 Location: Indonesia
Posts: 62
| Code: #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();
}
|
| Kinshara is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Permutations | bumfluff | C++ Programming | 2 | 10-05-2008 12:33 PM |
| recursive permutations | wd_kendrick | C Programming | 7 | 06-02-2008 03:11 PM |
| permutations of a string | agarwaga | C Programming | 1 | 05-23-2006 08:52 AM |
| permutations | packer | C Programming | 13 | 06-02-2005 12:54 AM |
| Permutations | kiddy | C++ Programming | 1 | 02-11-2002 09:53 AM |