Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ProblemA {
  4.  
  5. public static void main(String[] args) {
  6. int a, b, c, d, x;
  7. Scanner s = new Scanner(System.in);
  8.  
  9. while (true) {
  10. a = s.nextInt();
  11. b = s.nextInt();
  12. c = s.nextInt();
  13. d = s.nextInt();
  14. if (a == -1 && b == -1 && c == -1 && d == -1) break;
  15. x = solve(a, b, c, d);
  16. System.out.println(x);
  17. }
  18.  
  19. }
  20.  
  21. private static int solve(int a, int b, int c, int d) {
  22.  
  23. if (a == -1) {
  24. if (d - c == c - b && isInRange(b - (d - c))) return b - (d - c); //Arithmetic test
  25. 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
  26. else return -1;
  27. } else if (b == -1) {
  28. if (2*(d - c) == c - a && isInRange(a + (d - c))) return a + (d - c);
  29. else if (d % c == 0 && c % a == 0 && (d/c)*(d/c) == (c/a) && isInRange(a*(d/c))) return a*(d/c);
  30. else return -1;
  31. } else if (c == -1) {
  32. if (d - b == 2*(b - a) && isInRange(b + (b - a))) return b + (b - a);
  33. else if (d % b == 0 && b % a == 0 && (b/a)*(b/a) == (d/b) && isInRange(b*(b/a))) return b*(b/a);
  34. else return -1;
  35. } else { // d == -1
  36. if (c - b == b - a && isInRange(c + (b - a))) return c + (b - a);
  37. else if (c % b == 0 && b % a == 0 && c/b == b/a && isInRange(c*(b/a))) return c*(b/a);
  38. else return -1;
  39. }
  40. }
  41.  
  42. private static boolean isInRange(int x) {
  43. if (x >= 1 && x <= 1000000) return true;
  44. else return false;
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement