Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int n;
- int k;
- int a[100]={-1};
- bool used [100]={false};
- void show(){
- for(int i=1;i<=k;i++){
- cout<<a[i]<<" ";
- }
- cout<<endl;
- }
- void backtrack (int pos){
- if (pos==k+1){
- if (a[1]<a[2]) show();
- return ;
- }
- else {
- for(int i=1;i<=n;i++){
- if (!used[i]){
- used[i]=true;
- a[pos]=i;
- backtrack(pos+1);
- used[i]=false;
- }
- }
- }
- }
- int main(){
- cin>>n;
- cin>>k;
- if (k==1){
- for(int i=1;i<=n;i++){
- cout<<i<<endl;
- }
- }
- else backtrack(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment