给定一个含n(n>1)个正整数(小于10)的数组a,有相同元素,按字典序输出所有由这n个整数构成全排列。
输入格式:
第1行给出一个整数n。
第2行给出n个整数。
输出格式:
按字典序输出所有由这n个整数构成全排列
输入样例:
3
2 5 8
输出样例:
258 285 528 582 825 852
输入样例:
3
1 2 2
输出样例:
122 212 221
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
using namespace std;
#include<iostream>
#include<vector>
using namespace std;
vector<int> num;
vector<vector<int>> result;
vector<int> path;
void backtracking(int n,int index);
int main(){
int n = 0;
int m = 0;
cin>>n;
for(int i = 0;i < n;i++){
cin>>m;
num.push_back(m);
}
backtracking(n,0);
for(const auto &res:result){
for(const auto &nums : res){
cout<<nums;
}
cout<<" ";
}
cout<<"size"<<result.size();
return 0;
}
void backtracking(int n,int index){
if(path.size() == n){
result.push_back(path);
return;
}
for(int i = 0;i < n;i++){
int flag = 0;
for(const auto &pa : path){
if((pa - num[i]) == 0){
flag = 1;
}
}
if(flag == 0){
path.push_back(num[i]);
backtracking(n,0);
path.pop_back();
}
}
}
组合不用考虑顺序,排列需要