Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Q3 {
- public static void main(String[] args) {
- FlashLight[] flashLights = {new FlashLight("bigA", 120),
- new FlashLight("bigB", 180),
- new FlashLight("bigC", 50),
- new FlashLight("bigD", 1),
- new FlashLight("bigE", 10),
- new FlashLight("bigF", 5),
- new FlashLight("bigG", 1000)};
- func(flashLights, 301); //wrong, output bigA and bigb
- //threeFlashLights(flashLights, 301); //correct answer bigA, bigB, bigD
- }
- public static void func(FlashLight[] arr, double total){
- int count = 0;
- for(int i=0; i<arr.length-2; i++) {
- if(arr[i].getPrice()+arr[i+1].getPrice()+arr[i+2].getPrice() == total) {
- System.out.println(arr[i].getModel() + " " + arr[i+1].getModel() + " " + arr[i+2].getModel());
- }
- }
- }
- public static void threeFlashLights(FlashLight[] arr, double total){
- FlashLight flsh1 = arr[0], flsh2= arr[0], flsh3= arr[0];
- boolean runLoop = true;
- for (int i = 0; i < arr.length-2 && runLoop; i++) {
- flsh1 = arr[i];
- for (int j = i+1; j < arr.length-1 && runLoop ; j++) {
- flsh2 = arr[j];
- for (int k = j+1; k < arr.length && runLoop; k++) {
- flsh3 = arr[k];
- if(flsh1.getPrice() + flsh2.getPrice() + flsh3.getPrice() == total)
- runLoop = false;
- }
- }
- }
- if(!runLoop)
- System.out.println(flsh1.getModel() + " " + flsh2.getModel() + " " + flsh3.getModel());
- else
- System.out.println("nothing found");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement