#include <iostream>
#include <cmath>
using namespace std;
int total = 0;
template <typename T>
bool check(T nums[], int low, int hight)
{//查询是否与前面重复
for (int i = low; i < hight; i++)
{
if (nums[i] == nums[hight])
{
return false;
}
}
return true;
}
//数组元素交换
template <typename T>
void swap(T nums[],int low,int hight)
{
T temp = nums[low];
nums[low] = nums[hight];
nums[hight] = temp;
}
template <typename T>
void perm(T nums[], int low, int hight)
{
if (low == hight) { //
total++;//
cout << ' ' << total << " :";
for (int i = 0; i < hight; i++) {
cout << nums[i] << ' ';
}
cout << endl;
}
else {
for (int i = low; i < hight; i++)
{
if (check(nums, low, i))//没有重复元素
{
swap(nums, low, i);//
perm(nums, low + 1, hight);
swap(nums, low, i);//到最后两个元素开始调用该方法
}
}
}
}
int main() {
int nums[] = { 1,1,2,3 };
perm<int>(nums, 0, 4);
return 0;
}
03-18
2320

06-19
1230
