[Java] 数组-03 面向过程的约瑟夫环

package com.bjsxt.chap5;

public class Count3Quit {
    public static void main(String[] args) {
        boolean[] arr = new boolean[500];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = true;
        }

        int leftCount = arr.length;
        int countNum = 0;
        int index = 0;

        while (leftCount > 1) {
            if (arr[index] == true) {
                countNum++;
                if (countNum == 3) {
                    countNum = 0;
                    arr[index] = false;
                    leftCount--;
                }
            }

            index++;

            if (index == arr.length) {
                index = 0;
            }
        }

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == true) {
                System.out.println(i);
            }
        }
    }
}

posted @ 2013-11-25 12:49  小尼人00  阅读(165)  评论(0编辑  收藏  举报