Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1.  
  2. int getArraySum(int[] a) {
  3.     int sum = 0;
  4.     for (int i = 0; i < a.length; i++) {
  5.         sum = sum + a[i];
  6.     }
  7.     return sum;
  8. }
  9.  
  10. int[] invertArray(int[] a) {
  11.     int[] app = new int[a.length];
  12.     for (int i = 0; i < a.length; i++) {
  13.         if (a[i] == 0) {
  14.             app[i] = 1;
  15.         } else {
  16.             app[i] = 0;
  17.         }
  18.     }
  19.     return app;
  20. }
  21.  
  22. boolean check(int[] a, int[] b) {
  23.     int[] c = new int[b.length];
  24.     for (int i = 0; i < a.length; i++) {
  25.         c[i] = a[i] * b[i];
  26.     }
  27.  
  28.     int sum = getArraySum(c);
  29.  
  30.     if (sum % 10 == 0 && sum != 0) {
  31.         // guardo se la somma degli elementi non in a è pari
  32.         int[] c1 = invertArray(b);
  33.  
  34.         int sumNotA = 0;
  35.         int[] res = new int[c1.length];
  36.         for (int q = 0; q < b.length; q++) {
  37.             res[q] = a[q] * c1[q];
  38.         }
  39.         sumNotA = getArraySum(res);
  40.         if (sumNotA % 2 != 0) {
  41.             return true;
  42.         } else {
  43.             return false;
  44.         }
  45.  
  46.     } else {
  47.         return false;
  48.     }
  49.  
  50. }
  51.  
  52. boolean recursivePartition(int[] a, int[] b, int p) {
  53.     boolean cond = false;
  54.     if (p == 0) {
  55.         cond = check(a, b);
  56.     } else {
  57.         b[p - 1] = 0;
  58.         cond = recursivePartition(a, b, p - 1);
  59.         if (!cond) { // 2° call
  60.             b[p - 1] = 1;
  61.             cond = recursivePartition(a, b, p - 1);
  62.         }
  63.  
  64.     }
  65.  
  66.     return cond;
  67.  
  68. }
  69.  
  70. boolean strangePartition(int[] a) {
  71.     return recursivePartition(a, new int[a.length], a.length);
  72.  
  73. }
  74.  
  75. int[] a = {82, 17, 76, 18, 62};
  76. System.out.println(strangePartition(a));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement