Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 7, 2026 12:08
Show Gist options
  • Select an option

  • Save thinkphp/9f3c7bdb2b95ee175f1288e580e8e480 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/9f3c7bdb2b95ee175f1288e580e8e480 to your computer and use it in GitHub Desktop.
permutari_rec.cpp
#include <iostream>
#define SIZE 100
using namespace std;
int stack[SIZE],n;
int ok(int k) {
for(int i = 1; i < k; ++i) {
if(stack[i] == stack[k]) return 0;
}
return 1;
}
//k = 2
void bkt( int k ) {
for(int i = 1; i <= n; ++i) {
stack[ k ] = i;//stack[1] = 1;stack[2] = 2; stack[3] = 3
if( ok( k ) ) {
if( k == n ) {
for(int i = 1; i <= n; ++i) cout<<stack[i]<<" ";//1 2 3
cout<<endl;
} else {
bkt( k + 1 );//3
}
}
}
}
int main(int argc, char const *argv[])
{
cin>>n;
bkt( 1 );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment