80人围成一圈按3循环报数,直至留下最后一人,求他原来所在的位置

 1 /*
 2  * 10.    有80个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),
 3  * 凡报到3的人退出圈子,问最后留下的是原来第几号的那位
 4  */
 5 public class Homework10 {
 6     public static void main(String[] args) {
 7         //创建长度是80的数组
 8         int[] array = new int[80];//默认都是0,假设存在为0,不存在为1
 9         //一次循环移动一个位置
10         int index = 0;//当前移动到的位置
11         int count = 0;//计数:1~3
12         int leftNum = array.length;//剩余人数
13         while(leftNum > 1) {
14             //判断这个人是否退出游戏:
15             if(array[index] == 0) {//没有退出
16                 count++;//计数
17                 if(count == 3) {
18                     array[index] = 1;//去掉这个人
19                     leftNum--;//总人数-1
20                     count = 0;//计数器恢复为0
21                 }
22             }
23             index++;
24             if(index >= array.length) {
25                 index = 0;
26             }
27         }
28         
29         //找到是0的人
30         for(int i=0; i<array.length; i++) {
31             if(array[i] == 0) {
32                 System.out.println(i+1);
33             }
34         }
35     }
36 }
原文地址:https://www.cnblogs.com/alpha-cat/p/11242740.html