可以用递归:
#include <iostream>
template<class T>
inline void swap( T& a, T& b )
{
T tmp = a;
a = b;
b = tmp;
}
template<class T>
void Perm( T list[], int start, int end )
{
int i;
if ( start == end )
{
for ( i = 0; i <= end; i++ )
cout << list[i];
cout << endl;
}
else
for ( i = start; i <= end; i++ )
{
swap( list[start], list[i] );
Perm( list, start+1, end );
swap( list[start], list[i] );
}
}
void main()
{
int list[] = {1,2,3,4,5};
Perm( list, 0, 4 );
}