Advertisement
bokoness

Ex1

Jun 15th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1.   public static void win(int[] k) {
  2.        //finding the highest score path between the odd indexes and the even indexes
  3.        int countOdd = 0;
  4.        int countEven = 0;
  5.        boolean amirTookOddPath;
  6.        for (int i = 0; i < k.length; i++) {
  7.            if (i % 2 == 0)
  8.                countEven += k[i];
  9.            else
  10.                countOdd += k[i];
  11.        }
  12.        amirTookOddPath = countOdd >= countEven ? true : false;
  13.  
  14.        int min = 0, max = k.length - 1;
  15.        //the odd path is the winning path
  16.        while (min < max) {
  17.            //Amir's path - the highest count of values between the odd indexes and the even indexes
  18.            if (amirTookOddPath ? min % 2 != 0 : min % 2 == 0) {
  19.                System.out.println("Amir took " + k[min]);
  20.                min++;
  21.            } else {
  22.                System.out.println("Amir took " + k[max]);
  23.                max--;
  24.            }
  25.            //Tamar's path - the opposite path of Amir, always choosing the highest value between the two
  26.            if (k[max] > k[min]) {
  27.                System.out.println("Tamar took " + k[max]);
  28.                max--;
  29.            } else {
  30.                System.out.println("Tamar took " + k[min]);
  31.                min++;
  32.            }
  33.        }
  34.        System.out.println("Final score:");
  35.        System.out.println("Amir total " + (amirTookOddPath? countOdd : countEven));
  36.        System.out.println("Tamar total " + (amirTookOddPath? countEven : countOdd));
  37.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement