Untitled
unknown
plain_text
a year ago
811 B
7
Indexable
package lockerpuzzle;
public class Lockerpuzzle {
public static void main(String[] args) {
boolean[] lockers = new boolean[100];
// Simulate each student toggling the lockers.
for (int student = 1; student <= 100; student++){
for (int locker = student - 1; locker < 100; locker += student)
{
lockers[locker] = !lockers[locker]; // Toggle the locker (open if closed, close if open).
}
}
// Print the locker that are open.
for (int i = 0; i < 100; i++) {
if (lockers[i]) {
System.out.println("Locker " + (i + 1) + " is open");
}
}
}
}Editor is loading...
Leave a Comment