约瑟夫环

package 约瑟夫环;

import java.util.ArrayList;
import java.util.List;

public class 约瑟夫环 {

    public static void main(String[] args) {
    
        List<Integer> list = new ArrayList<Integer>();

        for (int i = 0; i < 8; i++) {
            list.add(i);
        }

        int index = -1;
        int sp = 3;

        while (list.size() >= sp) {
            index = (index + sp) % list.size();
            list.remove(index);
        }

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}

 

posted @ 2015-09-24 21:49  大数据从业者FelixZh  阅读(266)  评论(0编辑  收藏  举报