How many doors are open
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
There are 1 500 people crowded in a large room. Connected to the room is a long hallway containing 1 500 closed doors. The first person in the room begins by opening every door in the long hallway. The second person goes and closes every other door. The third person goes to every third door and opens it if it is closed and closes it if it is opened. The fourth person goes to every fourth door and does the same procedure and so on. How many doors are left open after the 1 500th person has completed this cycle?
3 Kommentare
Antworten (1)
David Hill
am 15 Sep. 2020
doors=zeros(1,1500);
for k=2:1500
doors(k:k:1500)=~doors(k:k:1500);
end
open=nnz(doors==0);
2 Kommentare
Stephen23
am 15 Sep. 2020
Without multiple implicit data type conversions:
doors = false(1,1500); % open=true
for k = 1:1500
doors(k:k:1500) = ~doors(k:k:1500);
end
open = nnz(doors);
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!