Advertisement
programajster

ALG_kosmonauci

Oct 24th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package kosmonauci;
  2.  
  3. public class Kosmonauci {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         int[][] kosmonauci = {{1, 0, 0, 1, 0}, //A
  8.         {0, 1, 1, 1, 0}, //B
  9.         {0, 0, 1, 0, 1}, //C
  10.         {1, 1, 0, 0, 1}}; //D
  11.         String podzbior = "{";
  12.         int a = 0;
  13.         int b = 0;
  14.         int c = 0;
  15.         int d = 0;
  16.         int ile = 0;
  17.         int ileMax = Integer.MAX_VALUE;
  18.  
  19.         final int N = kosmonauci[0].length; // moc zbioru
  20.         int[] tab = new int[N + 1];
  21.         int s = (int) Math.pow(2, N);
  22.         for (int l = 0; l < s; l++) {
  23.             for (int i = 0; i < N; i++) {
  24.  
  25.                 if (tab[i] == 1) {
  26.                     a += kosmonauci[0][i];
  27.                     b += kosmonauci[1][i];
  28.                     c += kosmonauci[2][i];
  29.                     d += kosmonauci[3][i];
  30.                     podzbior = podzbior + " " + (i + 1);
  31.                     ile++;
  32.                 }
  33.             }
  34.  
  35.             if (a > 0 && b > 0 && c > 0 && d > 0 && ile <= ileMax) {
  36.                 System.out.println(podzbior + "}");
  37.                 ileMax = ile;
  38.             }
  39.  
  40.             podzbior = "{";
  41.             a = 0;
  42.             b = 0;
  43.             c = 0;
  44.             d = 0;
  45.             ile = 0;
  46.  
  47.             int i = 0;
  48.             do {
  49.                 if (tab[i] == 1) {
  50.                     tab[i] = 0;
  51.                     i++;
  52.                 } else {
  53.                     tab[i] = 1;
  54.                     break;
  55.                 }
  56.             } while (true);
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement