Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add a perm

  • Loading branch information...
commit 1a07fd1dc97cbc4512c493e02550b92433a0d5df 1 parent 303b162
= authored
Showing with 24 additions and 2 deletions.
  1. +24 −2 fullPerm.cpp
View
26 fullPerm.cpp
@@ -3,17 +3,22 @@
用了递归的思想. 首先把数组分成第一个和从第二个开始两部分(子排列, 递归展开)。然后把第一个和子序列其中一个交换, 再排, 再排, 再排
*/
-#include<iostream>
+#include <iostream>
+#include <algorithm>
using namespace std;
void swap(int array[], int pos1, int pos2);
void FullPerm(int array[], int begin, int size);
+void FullPermStd(int array[], int begin, int size);
int main(void)
{
int array[] = {1,3,7,1,9};
- FullPerm(array, 0, sizeof(array)/sizeof(int));
+ FullPerm(array, 0, sizeof(array)/sizeof(array[0]));
+
+ int array2[] = {1,3,2};
+ FullPermStd(array2, 0, sizeof(array2)/sizeof(array2[0]));
return 0;
@@ -43,4 +48,21 @@ void swap(int array[], int pos1, int pos2)
int tmp = array[pos1];
array[pos1] = array[pos2];
array[pos2] = tmp;
+}
+
+
+//std提供了一个计算全排列的算法
+//首先把元素从小到大放好,然后反复调用next_permutation()
+void FullPermStd(int array[], int begin, int size)
+{
+ sort(array, array+size);
+ do
+ {
+ for(int i=0;i<size;i++)
+ {
+ cout<<array[i]<<" ";
+ }
+ cout<<endl;
+ }while(next_permutation(array, array+size));
+ //貌似只能是N个不同元素
}
Please sign in to comment.
Something went wrong with that request. Please try again.