direction of movement in program
Aug 3, 2010 at 1:57am Aug 3, 2010 at 1:57am UTC
i am trying to implement this program
http://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm
i get output as the numbers but not the arrows how do i program the directions because i tried everything and i couldnt figure it out i know its something so simple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#include <iostream>
using namespace std;
int n;
int i;
int count = 0;
int p[100];
int pi[100];
int dir[100];
void PrintPerm()
{
int i;
for (i=1; i <= n; ++i)
printf("%d" , p[i]);
}
void PrintTrans( int x, int y )
{
printf ( " <%d <%d " , x, y);
cout << "\n" ;
}
void Move( int x, int d )
{
int z;
PrintTrans( pi[x], pi[x]+d );
z = p[pi[x]+d];
p[pi[x]] = z;
p[pi[x]+d] = x;
pi[z] = pi[x];
pi[x] = pi[x]+d;
}
void perm(int s)
{
int i;
if ( s > n)
{
PrintPerm();
}
else
{
perm( s+1 );
for (i=1; i<= s-1; ++i)
{
Move( s, dir[s] );
perm( s+1 );
}
dir[s] = -dir[s];
}
}
int main()
{
cout << "Enter the numbers you want to compute: \n" ;
cin >> n;
cout <<"\n" ;
for (i=1; i<=n; ++i)
{
dir[i] = -1;
p[i] = i;
pi[i] = i;
}
perm (1);
cout << "\n" ;
return 0;
}
Topic archived. No new replies allowed.