public class test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
for(int i = 0;i<6;i++){
list.add("monkey"+i);
}
System.out.println(list);
int total = 6; //假设6只猴子
int target = 3; //数字为3;
int index = 0; //数组下标
int count = 0; //计数器
int reminder = total; //剩余猴子数量
while(reminder > 1){
//下标和猴子的数量相同时,把index置为0
if(index == reminder){
index = 0;
}
//报数的猴子部位null 计数器++
if(list.get(index) != null) {
count++;
//如果计数器为3,移除报数为3的猴子
if (count == target) {
list.remove(index);
reminder--;
count = 0;
continue;
}
} |
|