Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions algorithms/print_permutations_1_to_n.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 100000;
bool used[ NMAX + 1 ];
int v[ NMAX + 1 ];

ifstream fin( "permutari1.in" );
ofstream fout( "permutari1.out" );

void make_perm( int pos, int n ) {
int i;
if ( pos == n + 1 ) {
for ( i = 1; i <= n; i++ )
cout << v[ i ] << ' ';
cout << '\n';
return;
}

for ( i = 1; i <= n; i++ ) {
if ( !used[ i ] ) {
used[ i ] = true;
v[ pos ] = i;
make_perm( pos + 1, n );
used[ i ] = false;
}
}
}

int main() {
int n;

cout << "Input an integer: ";
cin >> n;

cout << "These are the permutations of the array [ 1, 2, 3, ..., n ]:\n";
make_perm( 1, n );

return 0;
}