約瑟夫環報數出列問題

public class Temp {

    public static void printYuesefuNum(int total, int k) {
        int[] data = new int[total];
        for (int i = 0; i < total; i++) {
            data[i] = i + 1;
        }

        int count = 0;
        int index = 0;
        int seq = 0;

        while (count < total) {
            if (index >= total) {
                index = 0;
            }

            if (data[index] == 0) {
                index++;
                continue;
            }

            seq++;
            if (seq == k) {
                System.out.println(data[index]);

                data[index] = 0;
                count++;
                seq = 0;
            }

            index++;
        }
    }

    public static void main(String[] args) {
        printYuesefuNum(7, 3);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章