fork download
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3.  
  4. bool isUnique(const char x[], const int y)
  5. {
  6. for (int i = 0; i < (y-1); i++)
  7. {
  8. for (int j = i+1; j < y; j++)
  9. {
  10. if (x[i] == x[j])
  11. {
  12. return false;
  13. }
  14. }
  15. }
  16.  
  17. return true;
  18. }
  19.  
  20. void perm(const char a[], char b[], const int x, const int y)
  21. {
  22. if ((y == x) && isUnique(b, x))
  23. {
  24. b[y] = '\0';
  25. printf("%s\n", b);
  26. }
  27. else
  28. {
  29. for (int i = 0; i < x; i++)
  30. {
  31. b[y] = a[i];
  32.  
  33. if (y < x)
  34. {
  35. perm(a, b, x, y+1);
  36. }
  37. }
  38. }
  39. }
  40.  
  41. int main(void)
  42. {
  43. const char a[] = { '1', '2', '3', '4' };
  44. char b[] = { '\0', '\0', '\0', '\0', '\0' };
  45.  
  46. perm(a, b, 4, 0);
  47. }
Success #stdin #stdout 0s 2248KB
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