500个人,一到三循环报数,数三的人退出,剩下一个人,是第几个?java程序实现

 

public class Count3Quit {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  boolean[] arr = new boolean[500];
  for (int i = 0; i < arr.length; i++) {
   arr[i] = true;
  }

  int leftCount = arr.length;
  int index = 0;
  int countNum = 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 on 2012-08-22 16:13  的的  阅读(251)  评论(0)    收藏  举报