选择性强迫症,选择性不能忽略任何一点瑕疵。
在网上下了 数据结构 朱仲涛版,实现了它上面的代码,这次是全排列。
#include<iostream>
using namespace std;
void SWAP(char &i,char &j,char &temp)
{
temp=i;
i=j;
j=temp;
}
void perm(char*list,int i,int n)
{
int j;
char temp;
if(i==n)
{
for(j=0;j<n;j++)
{
cout<<list[j]<<" ";
}cout<<list[j]<<endl;
}
else
{
for(j=i;j<=n;j++)
{
SWAP(list[i],list[j],temp);
perm(list,i+1,n);
SWAP(list[i],list[j],temp);
//定义两个SWAP的原因是要换回来,数组位置基本没变过。
//j==n的用处仅仅在于其中一个与最后一个交换
//i和j的过程(每个END之前都有一次交换,一种是有意义的,都是复原数组,另一种是没意义的)
/*perm(list,0,5);
perm(list,1,5);
perm(list,2,5);
perm(list,3,5);
perm(list,4,5);
perm(list,5,5);(输出a b c d e f)
END;
i=4<->j=5;
perm(list,5,5);输出(a b c d f e)
END;
End;
i=3<->j=4;
permu(list,4,5);(之后这个式子直接省略,以最后两个交换一次代替。)
perm(list,5,5);(输出a b c e d f)
END;
i=4<->j=5;
perm(list,5,5);(输出a b c e f d);
END;
End;
i=3<->j=5;
perm(list,4,5);
perm(list,5,5);(输出a b c f e d)
perm(list,5,5);(输出a b c f d e)
End;
End;
i=2<->j=3;
permu(list,3,5);
同上再来一次。
i=2<->j=4;
permu(list,3,5);
同上再来一次。
i=2<->j=5;
permu(list,3,5);
同上再来一次。
End;
i=1<->j=1;
perm(list,2,5);
同上再来一次;
i=1<->j=2;
perm(list,2,5);
同上再来一次;
i=1<->j=3;
perm(list,2,5);
同上再来一次;
i=1<->j=4;
perm(list,2,5);
同上再来一次;
i=1<->j=5;
perm(list,2,5);
同上再来一次;
End;
*/
}
}
}
int main()
{
char a[]={'a','b','c','d','e','f'};
perm(a,0,5);
return 0;
}