【Aspose.Words for Java】 對word文件,增加頁首,頁尾,插入內容區影象,
阿新 • • 發佈:2020-10-29
從n個不同元素中任取m(m ≤ n)個元素,按照一定的順序排列起來,叫做從 n 個不同元素中取出 m 個元素的一個排列。當m = n時所有的排列情況叫全排列。
以下舉例 1,2,3,4,5 的全排列解決方案;
1.for迴圈解決——直接迴圈,保證數字不一樣即可;
#include <iostream> using namespace std; void Full(int * a,int n) { for(int k1 = 0;k1 < n;k1++) { for(int k2 = 0;k2 < n;k2++) {if(k1 != k2) // ....... } } } int main() { int a[5] = {1,2,3,4,5}; Full(a,5); return 0; }
2.next_permutation()——可以查一下這個函式,下面實現全排列;
#include <iostream> #include <algorithm> using namespace std; void Full_next(int * a,int n) { sort(a,a+n); do {for(int i = 0;i < n;i++) cout << a[i] << " "; cout << endl; }while(next_permutation(a,a + n)); } int main() { int a[5] = {1,2,3,4,5}; Full_next(a,5); return 0; }
3.遞迴實現——加一點dfs思想,利用深度確定遞迴出口;
#include <iostream> using namespace std; voidSwap(int & x,int & y) { int temp = x; x = y; y = temp; } void Print(int * a,int n) { for(int i = 0;i < n;i++) cout<<a[i]<<" "; cout<<endl; } void Full_dfs(int * a,int n,int i) { if(i >= n) Print(a,n); else { for(int j = i;j < n;j++) { Swap(a[i],a[j]); Full_dfs(a,n,i + 1); Swap(a[j],a[i]); } } } int main() { int a[5] = {1,2,3,4,5}; Full_dfs(a,5,0); return 0; }