Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ProblemA {
- public static void main(String[] args) {
- int a, b, c, d, x;
- Scanner s = new Scanner(System.in);
- while (true) {
- a = s.nextInt();
- b = s.nextInt();
- c = s.nextInt();
- d = s.nextInt();
- if (a == -1 && b == -1 && c == -1 && d == -1) break;
- x = solve(a, b, c, d);
- System.out.println(x);
- }
- }
- private static int solve(int a, int b, int c, int d) {
- if (a == -1) {
- if (d - c == c - b && isInRange(b - (d - c))) return b - (d - c); //Arithmetic test
- else if (d % c == 0 && c % b == 0 && d/c == c/b && b % (d/c) == 0 && isInRange(b/(d/c))) return b/(d/c); //Geometric test
- else return -1;
- } else if (b == -1) {
- if (2*(d - c) == c - a && isInRange(a + (d - c))) return a + (d - c);
- else if (d % c == 0 && c % a == 0 && (d/c)*(d/c) == (c/a) && isInRange(a*(d/c))) return a*(d/c);
- else return -1;
- } else if (c == -1) {
- if (d - b == 2*(b - a) && isInRange(b + (b - a))) return b + (b - a);
- else if (d % b == 0 && b % a == 0 && (b/a)*(b/a) == (d/b) && isInRange(b*(b/a))) return b*(b/a);
- else return -1;
- } else { // d == -1
- if (c - b == b - a && isInRange(c + (b - a))) return c + (b - a);
- else if (c % b == 0 && b % a == 0 && c/b == b/a && isInRange(c*(b/a))) return c*(b/a);
- else return -1;
- }
- }
- private static boolean isInRange(int x) {
- if (x >= 1 && x <= 1000000) return true;
- else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement