AcWing 1537. 递归实现排列类型枚举 II
原题链接
简单
作者:
牛奶小柒Luke
,
2021-04-14 19:33:01
,
所有人可见
,
阅读 336
排列问题
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 10;
int n;
int a[N],nums[N];
bool st[N];
void dfs(int u){
if(u == n){
for(int i = 0;i < n;++i) printf("%d ",nums[i]);
puts("");
return;
}
for(int i = 0;i < n;++i){
if(!st[i]){
nums[u] = a[i];
st[i] = true;
dfs(u + 1);
st[i] = false;
while(i + 1 < n && a[i + 1] == a[i]) i++;
}
}
}
int main(){
scanf("%d",&n);
for(int i = 0;i < n;++i) scanf("%d",&a[i]);
sort(a,a + n);
dfs(0);
return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 10;
int n;
int a[N];
int main(){
scanf("%d",&n);
for(int i = 0;i < n;++i) scanf("%d",&a[i]);
sort(a,a + n);
do{
for(int i = 0;i < n;++i) printf("%d ",a[i]);
puts("");
}while(next_permutation(a,a + n));
return 0;
}