-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle_swich.java
More file actions
44 lines (32 loc) · 1.21 KB
/
toggle_swich.java
File metadata and controls
44 lines (32 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// You make 100 passes by the doors starting with the first door every time.
// The first time through, you visit every door and toggle the door
// (if the door is closed, you open it; if it is open, you close it).
// The second time you only visit every 2nd door (door #2, #4, #6, ...).
// The third time, every 3rd door (door #3, #6, #9, ...), etc., until you only visit the 100th door.
// Question: What state are the doors in after the last pass?
public class toggle_swich {
public static void main(String args[]){
int n = 100;
boolean[] doors = new boolean[n+1];
int c = 0;
int o = 0;
for (int i = 1; i <= n; i++) {
for (int j = i; j+i <= n; j++){
if (doors[j] == false) {
doors[j] = true;
} else {
doors[j] = false;
}
}
}
for (int i = 1; i <= n; i++) {
if (doors[i] == false) {
c++;
} else {
o++;
}
}
System.out.println("Open doors: " + o);
System.out.println("Closed doors: " + c);
}
}