博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

以数组的方法:

 

 

 public static void main(String[] args) {
        final int n = 10; 
        final int k = 1; 
        final int m = 3; 
        start(n, k, m); 
 }
    public static void start(int n, int k, int m){ 
        //生产数组 
        int[] arrays = new int[n]; 
        for (int i = 0; i < arrays.length; i++) 
            arrays[i] = i+1; 
        //开始弹出 
        int now = 0; 
        int num = k; //当前人 
  int j=1;
        while(j<=n){        
            if(arrays[num-1] != 0){ //如果当前没弹出 
             now ++ ;
                if(now == m){ //弹出 
                    System.out.println("pop:" + arrays[num-1]); 
                    arrays[num-1] = 0; 
     j++;
                    now = 0; 
                } 
            } 
            num ++ ; 
            if(num > n) 
                num = 1; 
        } 
         
    }