You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
821 B

#include<bits/stdc++.h>
using namespace std;
//打印数组全部元素
void prt(int arr[], int end) {
for (int i = 0; i <= end; ++i) {
printf("%d", arr[i]);
}
printf("\n");
}
//全排列 c++实现
//https://blog.csdn.net/u013309870/article/details/68941284
//https://blog.csdn.net/jiaobuchong/article/details/85369970
//https://www.cnblogs.com/kiritozhj/p/10501470.html
// https://blog.csdn.net/sofia_m/article/details/78865892
void perm(int arr[], int begin, int end) {
//递归出口,结束时打印结果
if (begin == end) {
prt(arr, end);
return;
}
//还没到出口时,就需要进行递归的递归体开发
for (int i = begin; i <= end; ++i) {
//交换两个元素值
swap(arr[begin], arr[i]);
//递归体
perm(arr, begin + 1, end);
//恢复原状
swap(arr[begin], arr[i]);
}
}
int main()
{
int arr[3] = { 3,1,2 };
perm(arr, 0, 2);
}