Advertisement
porteno

יגור

Dec 10th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1.  
  2. public class Q3 {
  3. public static void main(String[] args) {
  4. FlashLight[] flashLights = {new FlashLight("bigA", 120),
  5. new FlashLight("bigB", 180),
  6. new FlashLight("bigC", 50),
  7. new FlashLight("bigD", 1),
  8. new FlashLight("bigE", 10),
  9. new FlashLight("bigF", 5),
  10. new FlashLight("bigG", 1000)};
  11. func(flashLights, 301); //wrong, output bigA and bigb
  12. //threeFlashLights(flashLights, 301); //correct answer bigA, bigB, bigD
  13. }
  14. public static void func(FlashLight[] arr, double total){
  15. int count = 0;
  16. for(int i=0; i<arr.length-2; i++) {
  17. if(arr[i].getPrice()+arr[i+1].getPrice()+arr[i+2].getPrice() == total) {
  18.  
  19. System.out.println(arr[i].getModel() + " " + arr[i+1].getModel() + " " + arr[i+2].getModel());
  20. }
  21.  
  22. }
  23.  
  24.  
  25. }
  26.  
  27.  
  28.  
  29. public static void threeFlashLights(FlashLight[] arr, double total){
  30.  
  31. FlashLight flsh1 = arr[0], flsh2= arr[0], flsh3= arr[0];
  32. boolean runLoop = true;
  33. for (int i = 0; i < arr.length-2 && runLoop; i++) {
  34. flsh1 = arr[i];
  35. for (int j = i+1; j < arr.length-1 && runLoop ; j++) {
  36. flsh2 = arr[j];
  37. for (int k = j+1; k < arr.length && runLoop; k++) {
  38. flsh3 = arr[k];
  39. if(flsh1.getPrice() + flsh2.getPrice() + flsh3.getPrice() == total)
  40. runLoop = false;
  41. }
  42. }
  43. }
  44. if(!runLoop)
  45. System.out.println(flsh1.getModel() + " " + flsh2.getModel() + " " + flsh3.getModel());
  46. else
  47. System.out.println("nothing found");
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement