转载自:
数组的全排列_Dablelv的博客专栏-CSDN博客_求数组全排列https://blog.csdn.net/k346k346/article/details/51154786
不重复
#include<iostream>
#include<algorithm>
using namespace std;
void permutation(int array[],int len,int cnt)
{
if(cnt == len){
for(int i =0;i<len;i++){
cout<<array[i];
}
cout<<endl;
}
else{
for(int i = cnt;i < len; i ++){
bool flag = true;
for(int j =i +1;j<len;j++){
if(array[j]==array[i])
flag = false;
}
if(flag){
swap(array[cnt],array[i]);
permutation(array,len,cnt+1);
swap(array[cnt],array[i]);
}
}
}
}
int main()
{
int array[12] = {0,0,0,0,0,0,0,1,1,1,1,1};
permutation(array,12,0);
return 0;
}
重复
#include<iostream>
#include<algorithm>
using namespace std;
long long sum;
void permutation(int array[],int len,int cnt)
{
if(cnt == len){
for(int i =0;i<len;i++){
cout<<array[i];
}
cout<<endl;
}
else{
for(int i = cnt;i < len; i ++){
swap(array[cnt],array[i]);
permutation(array,len,cnt+1);
swap(array[cnt],array[i]);
}
}
}
int main()
{
int array[12] = {0,0,0,0,0,0,0,1,1,1,1,1};
permutation(array,12,0);
return 0;
}