public class TestLockers {
public static void main(String[] args) {
boolean[] isOpen = new boolean[1000];
//管理员打开
for(int i=0; iisOpen[i] = true;
}
//学生从第2个开始关闭相隔的门
for(int i=1; iisOpen[i] = false;
}
//校长从第3个柜子开始每三个柜子改变状态
for(int i=2; iif(isOpen[i]) {
isOpen[i] = false;
} else {
isOpen[i] = true;
}
}
//输出开着的柜子的号码
for(int i=0; iif(isOpen[i]) {
System.out.print((i+1) + " ");
}
}
}
}
public class TestNothing {
public static void main(String[] args) {
//初始化打开为false
boolean[] doors = new boolean[1000];
operate(doors, 2, 2);
operate(doors, 3, 3);
// operate(doors, 4, 4); //从第四个开始,每隔四个
// operate(doors, 5, 5); //从第五个开始,每隔五个
for (int i = 0; i < doors.length; i++)
{
System.out.println("Number."+(i+1)+":"+(doors[i]?"closed":"open"));
}
}
/**
* 对门的操作
* @param doors 所有的门
* @param from 从第几个开始
* @param distant 每隔几个操作
*/
public static void operate(boolean[] doors, int from, int distant)
{
int i = from;
while(i <= doors.length){
doors[i-1] = !doors[i-1];
i += distant;
}
}
}
int[] aaa = new int[1000];