|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | + |
| 5 | +public class Project { |
| 6 | + |
| 7 | + public static void main(String args[]) throws IOException |
| 8 | + { |
| 9 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + int[] arr = new int[4]; |
| 11 | + String[] arr2 = new String[4]; |
| 12 | + String line = ""; |
| 13 | + int place = -1; |
| 14 | + double answer; |
| 15 | + boolean stop; |
| 16 | + |
| 17 | + outer: |
| 18 | + while((line = br.readLine()) != null) |
| 19 | + { |
| 20 | + stop = true; |
| 21 | + arr2 = line.split(" "); |
| 22 | + |
| 23 | + for(int i = 0; i < 4; i++) |
| 24 | + { |
| 25 | + arr[i] = Integer.parseInt(arr2[i]); |
| 26 | + if(arr[i] == -1) |
| 27 | + place = i; |
| 28 | + if(arr[i] != -1) |
| 29 | + stop = false; |
| 30 | + } |
| 31 | + |
| 32 | + if(stop) |
| 33 | + break; |
| 34 | + |
| 35 | + answer = isArithmetic(arr, place); |
| 36 | + if(answer == -1) |
| 37 | + answer = isGeometric(arr, place); |
| 38 | + |
| 39 | + if(errorCheck(answer)) |
| 40 | + System.out.println((int)answer); |
| 41 | + else |
| 42 | + System.out.println(-1); |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static double isGeometric(int[] arr, int place) |
| 50 | + { |
| 51 | + double r = -1; |
| 52 | + double ans = -1; |
| 53 | + |
| 54 | + switch(place) |
| 55 | + { |
| 56 | + case 0: |
| 57 | + r = (double) arr[2] / arr[1]; |
| 58 | + if(r * arr[2] == arr[3]) |
| 59 | + ans = arr[1] / r; |
| 60 | + break; |
| 61 | + case 1: |
| 62 | + r = (double) arr[3] / arr[2]; |
| 63 | + if(arr[2] * Math.pow(r, 2) == arr[0]) |
| 64 | + ans = arr[2] / r; |
| 65 | + break; |
| 66 | + case 2: |
| 67 | + r = (double) arr[1] / arr[0]; |
| 68 | + if(arr[1] * Math.pow(r, 2) == arr[3]) |
| 69 | + ans = arr[3] / r; |
| 70 | + break; |
| 71 | + case 3: |
| 72 | + r = (double) arr[1] / arr[0]; |
| 73 | + if(arr[1] * r == arr[2]) |
| 74 | + ans = arr[2] * r; |
| 75 | + break; |
| 76 | + } |
| 77 | + return ans; |
| 78 | + } |
| 79 | + |
| 80 | + public static double isArithmetic(int[] arr, int place) |
| 81 | + { |
| 82 | + int dif = -1; |
| 83 | + double ans = -1; |
| 84 | + switch(place) |
| 85 | + { |
| 86 | + case 0: |
| 87 | + dif = arr[2] - arr[1]; |
| 88 | + if(dif + arr[2] == arr[3]) |
| 89 | + ans = arr[1] - dif; |
| 90 | + break; |
| 91 | + case 1: |
| 92 | + dif = arr[3] - arr[2]; |
| 93 | + if(arr[2] - (2 * dif) == arr[0]) |
| 94 | + ans = arr[2] - dif; |
| 95 | + break; |
| 96 | + case 2: |
| 97 | + dif = arr[1] - arr[0]; |
| 98 | + if(arr[1] + (2*dif) == arr[3]) |
| 99 | + ans = arr[3] - dif; |
| 100 | + break; |
| 101 | + case 3: |
| 102 | + dif = arr[1] - arr[0]; |
| 103 | + if(arr[1] + dif == arr[2]) |
| 104 | + ans = arr[2] + dif; |
| 105 | + break; |
| 106 | + } |
| 107 | + return ans; |
| 108 | + } |
| 109 | + |
| 110 | + public static boolean errorCheck(double num) |
| 111 | + { |
| 112 | + if(num >= 1 && num <= 1000000 && (Math.floor(num) == num)) |
| 113 | + return true; |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments