#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void Permutation(int list[], int low, int high)
{
if (low == high)//當low==high時,此時list就是其中一個排列,輸出list
{
for (int i = 0; i <= low; i++)
cout << list[i];
cout << endl;
}
else
{
for (int i = low; i <= high; i++)//每個元素與第一個元素交換
{
swap(list[i], list[low]);
Permutation(list, low + 1, high); //交換後,得到子序列,用函數Permutation得到子序列的全排列
swap(list[i], list[low]);//最後,將元素交換回來,復原,然後交換另一個元素
}
}
}
int main()
{
int list[] = { 1, 2, 3 };
Permutation(list, 0, 2);
system("pause");
return 0;
}
全排列Permutation遞歸方法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.