fork download
  1. import java.util.Stack;
  2.  
  3. class Test {
  4.  
  5. public static void main(String[] args) {
  6. int[] input = { 1, 2, 3, 4, 5 };
  7. ma(input);
  8. }
  9.  
  10. public static void ma(int[] input) {
  11. Stack<Boolean> stack = new Stack<>();
  12. while (true) {
  13. while (stack.size() < input.length) {
  14. stack.push(true);
  15. print(stack, input);
  16. }
  17. while (!stack.isEmpty() && !stack.peek())
  18. stack.pop();
  19. if (stack.isEmpty())
  20. break;
  21. stack.pop();
  22. stack.push(false);
  23. }
  24. }
  25.  
  26. public static void print(Stack<Boolean> stack, int[] input) {
  27. boolean begin = true;
  28. for (int i = 0; i < stack.size(); i++)
  29. if (stack.get(i)) {
  30. if (begin)
  31. begin = false;
  32. else
  33. System.out.print(' ');
  34. System.out.print(input[i]);
  35. }
  36. System.out.println();
  37. }
  38. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 5
1 2 4
1 2 4 5
1 2 5
1 3
1 3 4
1 3 4 5
1 3 5
1 4
1 4 5
1 5
2
2 3
2 3 4
2 3 4 5
2 3 5
2 4
2 4 5
2 5
3
3 4
3 4 5
3 5
4
4 5
5