hello, dear all. i'have small problem. here is my algorithm
#include <stdio.h>
/**
Read a number, n, from standard input and print the
permutations.
*/
void write( int *x, int n)
{
if (x != 0) {
for (int i = 0; i < n; i++) {
printf("%4d", x[i] );
}
printf("\n");
}
} // print
void swap(int *x, int i, int j)
{
int t;
t = x[i];
x[i] = x[j];
x[j] = t;
}
void recycle(int *x, int start, int n)
{
int tmp = x[start+1];
for (int i = start+1; i <n-2; ++i) {
x[i] = x[n-i-1];
x[n-i-1] = tmp;
}
}
void starterequiv(int *x, int start, int n)
{
write(x, n);
if (start < n) {
int i, j;
for (i = n-1; i > start; i--) {
for (j = i + 1; j < n; j++) {
swap(x, i, j);
starterequiv(x, i, n);
}
recycle(x, i, n);
}
}
}
void initiate(int *x, int n)
{
for (int i = 0; i <n; i++) {
x[i] = i+1;
}
} // init
int main()
{
char buf[100];
int n;
printf("Enter the number of elements: ");
fgets(buf, sizeof(buf), stdin );
sscanf_s(buf, "%d", &n);
if (n > 0 && n <= 100) {
int *x = new int[n];
initiate(x, n);
starterequiv(x, 0, n);
}
}
The problem is
i didn;t get an output same as follows (n=4)
1234
1432 (recycle)
1423 (swap)
1324 (recycle)
1342 (swap)
1243. ( recycle).
Its based combinatoric concept. how to fix it?
What exactly do you want to do, what is the output and what would you expect it to be? Do you want *all* permutations of some input digits? (That's what the text says, however, for 4 you have 4! = 24 permutations, you only list 5). A little bit more explanation and a little bit less not-working source code would perhaps be helpful.
ok. thanks for the reply me. my algorithm output as follows: n= 4
1234
1243
1423
1432
1234
1243.
i didn;t need all 24 permutation. i just need first 6 of 24 permutation which begin with element '1'. i knew that we can solve using swap and recursion.
i need to program which swap and reverse is run by side by side
recycle and swap in loop. i think the problem is at starterequiv function.
Alternatively the STL provides all of this for you:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <algorithm>
#include <iostream>
#include <string>
int main() {
// Assume this is what you want to permute:
std::string numbers( "4321" );
// next_permutation requires the first permutation to be in sorted order
std::sort( numbers.begin(), numbers.end() );
// The sorted sequence is the first permutation
std::cout << numbers << std::endl;
// Loop through all permutations...
while( std::next_permutation( numbers.begin(), numbers.end() ) )
std::cout << numbers << std::endl;
}
ok.its help me so much. but i have to use my own algorithm in spite of next-permutation. Maybe its quite difficult to do loop swap and reverse. that's ok. here is my output from the algorithm. i just generate 6.
1234
1243
1324
1342
1423
1432.
I just need there first three. how to use filter/erase/delete function to delete the last three.then my output will become as follows:
void starterequiv(int *arr, int start, int SIZE)
{ int count = 1;
if (start > SIZE)
print(arr, SIZE);
else
{ int i, j;
starterequiv(arr,start+1 ,SIZE);
for (i = SIZE-1; i > start; i--) {
for (j = i+1 ; j <= SIZE-1; j++) {
swap(arr, i, j);
starterequiv(arr, i, SIZE);
count++ ;
}
shiftleft(arr,i,SIZE);
}
if (count == 4) exit(0);
}
}
my output:
Enter the number of elements: 4
1234
1243
1243
1324
1342
Press any key to continue . . .
the are two problem:
(i) repeated permutation.
and the second one is if (count == 4) exit(0); is not function. for to list the first 4 of the permutation.
Sorry, misunderstood what your program did. If thats the case just move the for() inside the starterequiv instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void starterequiv(int *arr, int start, int SIZE)
{
int count = 0;
for (count; count <=4; i++) {
if (start > SIZE) {
print(arr, SIZE);
} else {
int i, j;
starterequiv(arr,start+1 ,SIZE);
for (i = SIZE-1; i > start; i--) {
for (j = i+1 ; j <= SIZE-1; j++) {
swap(arr, i, j); } }
starterequiv(arr, i, SIZE);
count++ ;
}
shiftleft(arr,i,SIZE);
}
This way the loop is inside the void function and only goes 4 times, this should work.