fork download
  1. #include <iostream>
  2.  
  3. void print( unsigned int n, const unsigned int A[] )
  4. {
  5. for( unsigned int i = 0 ; i <= n ; ++i ) std::cout << A[i] << ' ' ;
  6. std::cout << '\n' ;
  7. }
  8.  
  9. void print_partitions( unsigned int n, unsigned int A[], unsigned int j = 0 )
  10. {
  11. if( n > 0 )
  12. {
  13. A[j] = n ;
  14. print( j, A ) ;
  15. for( unsigned int i = j ? A[j-1] : 1 ; i <= n/2 ; ++i )
  16. {
  17. A[j] = i ;
  18. print_partitions( n-i, A, j+1 ) ;
  19. }
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. constexpr unsigned int N = 6 ;
  26. unsigned int A[N] ;
  27. print_partitions( N, A );
  28. }
  29.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
6 
1 5 
1 1 4 
1 1 1 3 
1 1 1 1 2 
1 1 1 1 1 1 
1 1 2 2 
1 2 3 
2 4 
2 2 2 
3 3