winfwu

导航

99个人围成圈开始数数,遇到3则退出,最后留下来的是谁

package TestFor0322;

public class Demo2For循环取数 {

    /**
     * 99个人围成圈开始数数,遇到3则退出,最后留下来的是谁?
     */
    public static void main(String[] args) {
        int n=99;
        loopFetch(n);
    }

    private static void loopFetch(int n) {
        boolean []b=new boolean[n];
        int index=0;//循环计数
        int leftcount=n;//数组中true数目
        int count=0;//统计连续出现3次true
        //数组初始化
        for(int i=0;i<n;i++){
            b[i]=true;
        }
        //主方法函数
        while(leftcount>1){
            if(b[index]==true){
                count++;
                if(count==3){
                    b[index]=false;
                    count=0;
                    leftcount--;
                }
            }
            index++;
            if(index==n){
                index=0;
            }
        }
        //找到最终为true的下标
        for(int i=0;i<n;i++){
            if(b[i]==true){
                System.out.print(i);
            }
        }
    }
}

 

posted on 2013-03-22 17:23  winfwu  阅读(392)  评论(0编辑  收藏  举报