妖碧落

导航

约瑟夫环

import java.util.ArrayList;

public class test7 {
    /*
     * 约瑟夫环
     */
    public static void main(String[] args) {
        System.out.println(getluck(8));
    }

    public static int getluck(int n) {
        ArrayList<Integer> a1 = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            a1.add(i);
        }
        int count = 1;
        for (int i = 0; a1.size()!=1; i++) {
            if (i==a1.size()) {
                i = 0;
            }
            if (count %3==0) {
                a1.remove(i);
                i--;
            }
            if (a1.size()==1) {
                break;
            }
            count++;            
        }
        return a1.get(0);
        
    }

}

 

posted on 2019-08-08 23:08  妖碧落  阅读(120)  评论(0编辑  收藏  举报