fork(1) download
  1. #include <stdio.h>
  2.  
  3. void printSolution(int* array, int* solution, int size)
  4. {
  5. for(int i = 0; i < size; i++)
  6. {
  7. printf("{ ");
  8. for(int j = 0; j < size; j++)
  9. {
  10. if(solution[j] == i + 1)
  11. {
  12. printf("%d ", array[j]);
  13. }
  14. }
  15. printf("} ");
  16. }
  17. printf("\n");
  18. }
  19.  
  20.  
  21. void printPartitions(int* array, int* solution, int size, int step, int maximum)
  22. {
  23. if(step == size) // no partitions left
  24. {
  25. printSolution(array, solution, size);
  26. }
  27. else
  28. {
  29. for(solution[step] = 1; solution[step] <= maximum; ++solution[step])
  30. {
  31. printPartitions(array, solution, size, step + 1, maximum);
  32. }
  33. printPartitions(array, solution, size, step + 1, maximum + 1);
  34. }
  35. }
  36.  
  37. int main(int argc, char const *argv[])
  38. {
  39. int array[] = {1, 2, 3, 4};
  40. int solution[4];
  41.  
  42. printPartitions(array, solution, 4, 0, 0);
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
{ 1 2 3 4 } { } { } { } 
{ 1 2 3 } { 4 } { } { } 
{ 1 2 4 } { 3 } { } { } 
{ 1 2 } { 3 4 } { } { } 
{ 1 2 } { 3 } { 4 } { } 
{ 1 3 4 } { 2 } { } { } 
{ 1 3 } { 2 4 } { } { } 
{ 1 3 } { 2 } { 4 } { } 
{ 1 4 } { 2 3 } { } { } 
{ 1 } { 2 3 4 } { } { } 
{ 1 } { 2 3 } { 4 } { } 
{ 1 4 } { 2 } { 3 } { } 
{ 1 } { 2 4 } { 3 } { } 
{ 1 } { 2 } { 3 4 } { } 
{ 1 } { 2 } { 3 } { 4 }