Program output is incorrect
Hey all, I’m currently in the midst of solving a problem, however after being stuck for over 2 weeks with no way out I have decided to turn to stack overflow.
Problem:
A bus tour of Europe has been very successful. Due to an increase in the number of people that want to go on a tour, the tour company decided to increase the height of the bus. The new height of the bus is exactly N centimeters.
But the tour’s route runs under a lot of bridges, and there is a chance that the bus will crash into one of these bridges. Can you find out if this will happen?
The first line of the input contains the height of the bus and the number of bridges under which the bus passes. The second line contains the heights of these bridges.
You should output "Will not crash" if everything will be all right; otherwise, output "Will crash on bridge i" (where i is a number of a bridge) into which the bus will crash. If the height of a bridge equals the height of the bus, the bus will crash.
Code:
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int busHeight = s.nextInt(); int bridgeCount = s.nextInt(); int bridgeHeight; boolean willCrash = false; int count = 0; for (int i = 0; i < bridgeCount; i++) { bridgeHeight = s.nextInt(); if (bridgeHeight == busHeight || bridgeHeight < busHeight) { willCrash = true; count++; } if (!(bridgeCount - count != 0 && willCrash)) { System.out.println("Will not crash"); break; } else { System.out.println("Will crash on bridge " + (bridgeCount - count)); break; } } } }
Sample Input: 234 8 465 453 981 463 1235 871 475 981
Correct Output: Will not crash
My output: Will not crash
Sample input 2: 211 5 871 205 123 871 1681
Sample output: Will crash on bridge 2
My Output: Will not crash
Let’s go through your code step by step…
.... for (int i = 0; i < bridgeCount; i++) { bridgeHeight = s.nextInt();
This makes sense!
if (bridgeHeight == busHeight || bridgeHeight < busHeight) { willCrash = true; count++; }
-
You can cut out some of the extra work here by using the <= operator!
-
The number of bridges is not going to change. We should probably not be updating the count variable here.
if (!(bridgeCount - count != 0 && willCrash)) { System.out.println("Will not crash"); break;
It looks like you are still in your for loop! We can’t know for sure that we won’t crash until we get through all the other bridges.
} else { System.out.println("Will crash on bridge " + (bridgeCount - count)); break; }
I am not sure why you decided to use bridgeCount – count as your bridge index. The i in the for loop we constructed should keep track of what bridge we are on!
Let’s try and write some revised code:
for (int bridgeIndex = 0; bridgeIndex < bridgeCount; bridgeIndex++) { bridgeHeight = s.nextInt(); if (bridgeHeight <= busHeight) { willCrash = true; // We know where our bus will crash. Let's abandon ship now! System.out.println("Will crash on bridge " + bridgeIndex); break; } } // end our for loop here! if (!willCrash) { // We got through every element of the for loop and none of them caused a crash! System.out.println("Will not crash."); }