Advertisement
fewfw

Untitled

Jun 25th, 2020
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4.  
  5. public class Main {
  6.  
  7.     public static int next_mask(int x){
  8.         int a = x & -x;
  9.         int b = x + a;
  10.         int c = b ^ x;
  11.         a <<= 2;
  12.         c /= a;
  13.         return b | c;
  14.     }
  15.  
  16.     public static void main(String[] args) {
  17.         int deep = 3;
  18.         int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  19.         List<int[]> list = new ArrayList<>();
  20.         int current = (1 << deep) - 1;
  21.         int last = 1 << array.length;
  22.         do {
  23.             int pos = 0;
  24.             int index = 0;
  25.             int[] res = new int[deep];
  26.             for (int a = current; a != 0; a >>= 1){
  27.                 if ((a & 1) == 1) res[index++] = array[pos];
  28.                 pos++;
  29.             }
  30.             list.add(res);
  31.             current = next_mask(current);
  32.         } while (current < last);
  33.         for (int[] k : list) System.out.println(Arrays.toString(k));
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement