Skip to content

Instantly share code, notes, and snippets.

@alirezaomidi
Last active August 29, 2015 14:18
Show Gist options
  • Save alirezaomidi/dbc6b2888245d786bd72 to your computer and use it in GitHub Desktop.
Save alirezaomidi/dbc6b2888245d786bd72 to your computer and use it in GitHub Desktop.
Prints $digits-permutation of 1 to $digits recursively
#include <stdio.h>
int digits, level;
int number[20];
int used[10];
void print() {
for (int i = 0; i < digits; i++)
printf("%d", number[i]);
printf("\n");
}
void gen() {
if (level == digits) {
print();
return;
}
for (int i = 1; i <= digits; i++) {
if (used[i]) continue;
number[level] = i;
used[i] = 1;
level++;
gen();
level--;
used[i] = 0;
}
}
int main() {
scanf("%d", &digits);
gen();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment