fork download
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3.  
  4. bool isUnique(char x[], int y)
  5. {
  6. for (int i = 0; i < (y-1); i++)
  7. for (int j = i+1; j < y; j++)
  8. if (x[i] == x[j])
  9. return false;
  10.  
  11. return true;
  12. }
  13. void perm(char a[], char b[], int x, int y)
  14. {
  15. if ((y == x) && isUnique(b,x))
  16. {
  17. b[y] = '\0';
  18. printf("%s\n", b);
  19. }
  20. else
  21. {
  22. for (int i = 0; i < x; i++)
  23. {
  24. b[y] = a[i];
  25. if (y < x)
  26. perm(a,b,x,y+1);
  27. }
  28. }
  29. }
  30. int main(void)
  31. {
  32. char a[] = {'1','2','3','4'};
  33. char b[] = {'\0','\0','\0','\0','\0'};
  34. perm(a,b,4,0);
  35. return 0;
  36. }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321