【约瑟夫环】java 数组实现

1 实现 源码

package com.xiaodaka.rec.data.refresh.lc;

public class YueSeFu {

    private static int[] peopleCircle = new int[41];

    static {
        for(int i = 0; i < 41; i++){
            peopleCircle[i] = 1;
        }
    }

    public static void main(String[] args) {
        int counter = 0;
        int number = 0;
        for(int i = 0; i< 41; i++){
            if(++number == 41){
                number = 0;
            }
            if(peopleCircle[number] == 0){
                i--;
                continue;
            }
            if(peopleCircle[number] == 1 && ++counter == 3 ){
                peopleCircle[number] = 0;
                System.out.print(number);
                System.out.print("-->");
                counter = 0;
            }else{
                i--;
            }
        }

    }
}

2 结果

3-->6-->9-->12-->15-->18-->21-->24-->27-->30-->33-->36-->39-->1-->5-->10-->14-->19-->23-->28-->32-->37-->0-->7-->13-->20-->26-->34-->40-->8-->17-->29-->38-->11-->25-->2-->22-->4-->35-->16-->31-->
Process finished with exit code 0

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页