Guest User

Untitled

a guest
Mar 6th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package deposit;
  7.  
  8. /**
  9. *
  10. * @author 100622796
  11. */
  12. import java.util.*;
  13.  
  14. public class Deposit {
  15.  
  16. /**
  17. * @param args the command line arguments
  18. */
  19. int x0, x1, y;
  20. boolean extracted;
  21.  
  22. public Deposit(int x0, int x1, int y) {
  23. this.x0 = x0;
  24. this.x1 = x1;
  25. this.y = y;
  26. }
  27.  
  28. public static boolean isBetween(int startX, int startY, int endX, int endY, Deposit deposit) {
  29. double slope, b;
  30.  
  31. if (startY != endY) {
  32. slope = (double) (endY - startY) / (double) (endX - startX); // calculate slope of the well
  33. b = startY - (double) (slope * startX); // calculate intercept
  34. double depoPoint = (deposit.y - b) / slope;
  35. depoPoint = Math.round(depoPoint);
  36. return (depoPoint >= deposit.x0 && depoPoint <= deposit.x1);
  37. }
  38. return false;
  39. }
  40.  
  41. public static void drillWell(Deposit[] depoArray) {
  42. int totalOil = 0;
  43. ArrayList<Integer> oilList = new ArrayList<Integer>();
  44.  
  45. int pointArray[] = new int[depoArray.length * 2];
  46. for (int i = 0; i < pointArray.length; i = i + 2) {
  47. pointArray[i] = depoArray[i / 2].x0;
  48. pointArray[i + 1] = depoArray[i / 2].x1;
  49. }
  50. for (int i = 0; i < pointArray.length; i++) {
  51. totalOil = 0;
  52. if (i % 2 == 0) {
  53. for (int j = i + 2; j < pointArray.length; j++) {
  54. totalOil = 0;
  55. for (int k = 0; k < depoArray.length; k++) {
  56. if (isBetween(pointArray[i], depoArray[(i / 2)].y, pointArray[j], depoArray[j / 2].y, depoArray[k]) && !(depoArray[i / 2].y == depoArray[j / 2].y)) {
  57. totalOil = totalOil + (depoArray[k].x1 - depoArray[k].x0);
  58. oilList.add(totalOil);
  59. }
  60. }
  61.  
  62. }
  63. } else {
  64. for (int j = i + 1; j < pointArray.length; j++) {
  65. totalOil = 0;
  66. for (int k = 0; k < depoArray.length; k++) {
  67. if (isBetween(pointArray[i], depoArray[(i / 2)].y, pointArray[j], depoArray[j / 2].y, depoArray[k]) && !(depoArray[i / 2].y == depoArray[j / 2].y)) {
  68. totalOil = totalOil + (depoArray[k].x1 - depoArray[k].x0);
  69. oilList.add(totalOil);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. System.out.println("largest value = " + Collections.max(oilList));
  76. }
  77.  
  78. /*
  79. public static void sort(Deposit arr[]) {
  80. int n = arr.length;
  81. for (int i = 1; i < n; ++i) {
  82. int key = arr[i].y;
  83. int j = i - 1;
  84.  
  85. /* Move elements of arr[0..i-1], that are
  86. greater than key, to one position ahead
  87. of their current position
  88. while (j >= 0 && arr[j].y > key) {
  89. arr[j + 1].y = arr[j].y;
  90. j = j - 1;
  91. }
  92. arr[j + 1].y = key;
  93. }
  94. } */
  95.  
  96. public static void main(String[] args) {
  97. Scanner input = new Scanner(System.in);
  98. System.out.println("Enter the number of deposits.");
  99. int numOfDeposits = input.nextInt();
  100. Deposit[] deposits = new Deposit[numOfDeposits];
  101. int start, finish, temp;
  102.  
  103. System.out.println("Enter the information of each deposit.");
  104. for (int i = 0; i < numOfDeposits; i++) {
  105. start = input.nextInt();
  106. finish = input.nextInt();
  107. if (start > finish) {
  108. temp = start;
  109. start = finish;
  110. finish = temp;
  111. }
  112. deposits[i] = new Deposit(start, finish, input.nextInt());
  113. }
  114. drillWell(deposits);
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment