Files
python/LuoGu/RuMen/P_001.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

35 lines
954 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
/**
* 功能:求数组元素的全排列
* 作者:黄海
* 时间2019-11-27
* @param list
* @param low
* @param high
*/
void perm(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]);
perm(list, low + 1, high); //交换后,得到子序列,用函数perm得到子序列的全排列
swap(list[i], list[low]);//最后,将元素交换回来,复原,然后交换另一个元素
}
}
}
int main() {
//读入输出优化的强迫症
ios::sync_with_stdio(false);
int s[] = {1,2,3,4,5};
perm(s,0,4);
return 0;
}