perl的Math::Combinatorics模块实现全排列

来源:互联网 发布:质数算法 编辑:程序博客网 时间:2024/05/22 06:53

代码:

#!/usr/bin/perl -wuse Math::Combinatorics qw(permute);@a = (1,2,3,4);foreach(permute(@a)){        print "@$_\n";}

permute()返回的是一个数组,该数组的元素为所有排列的数组的引用。

输出为:

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1


0 0
原创粉丝点击