有500个小朋友编号1-500  围成一圈,做1-3数数小游戏。 数到3的时候 那位小朋友推出,一直循环,算出最后一个小朋友的编号。

解决方案 »

  1.   

    约瑟夫环问题:http://topic.csdn.net/u/20120627/20/4f300b35-b616-4d3d-8a22-e5313f9fc3cb.html
      

  2.   

    按你的要求写的,记得给我点分哈。
    public class test2

    static {System.out.println("aaaaaaaaaa");}
    public static void main(String[] args)
    {
    //创建1-500个小朋友编号
    int[] childs=new int [500];
    for(int i=0;i<500;i++){
    childs[i]=i+1;
    }
    //判断数到了3的小朋友编号
    int iNum=0;
    for(int i=0;i<500;i++){
    iNum++;
    if(iNum==3){
    System.out.println("数到3的小朋友编号是"+childs[i]);
    iNum=0;
    }
    }

    }
    }
      

  3.   

    呵呵..  楼主试试我的...import java.util.Arrays;
    public class TestString {
    public static void main(String[] args) {
    boolean[] c = new boolean[500];
    Arrays.fill(c, true);
    int count = 0;
    int num = 0;
    for (int i=0; true; i++) {
    if (!c[i % c.length]) {
    i++;
    continue;
    }
    if (++count % 3 == 0) {
    c[i % c.length] = false;
    num++;
    }
    if (num == 499) {
    break;
    }
    }

    for (int i = 0; i < c.length; i++) {
    if (c[i]) {
    System.out.println("最后一个小朋友的编号是" + (i + 1) );
    }
    }
    }
    }
      

  4.   


    package com.zf.test;import java.util.LinkedList;
    import java.util.List;public class Test3 {

    //虚拟 n 个children
    public List<Integer> buildChildren(int childrenSize){
    List<Integer> children = new LinkedList<Integer>();
    for (int i = 1 ; i <= childrenSize; i++) {  
    children.add(i);
    }
    return children;



    //递归计算
    public int calc (int currentNo , int currentListIndex , List<Integer>  children){
    if(currentListIndex == children.size()){
    if(children.size() > 1){
    currentListIndex = 0 ;
    }else{
    return children.get(0);  
    }
    }

    if(currentNo == 3){
    children.remove(currentListIndex);
    currentNo = 1;
    }else{
    currentNo++;
    currentListIndex++;
    }
    return calc(currentNo , currentListIndex , children) ;
    } //包装
    public int run( int childrenSize){
    return calc(1 , 0 , buildChildren(childrenSize));
    }

    public static void main(String[] args) {

    Test3 t = new Test3(); 
    System.out.println("最后退出的小朋友为:" + t.run(500));       

    }

    }
      

  5.   

    面向对象的:
    package com.test;public class CountQuit {
    public static void main(String args[]) {
    KidCircle kc = new KidCircle(500);
    int countNum = 0;
    Kid k = kc.first;
    while(kc.count > 1) {
    countNum ++ ;
    if(countNum == 3) {
    countNum = 0;
    kc.delete(k);
    }
    k = k.right;
    }
    System.out.println(kc.first.id);
    }
    }class Kid {
    int id;
    Kid left;
    Kid right;
    }class KidCircle {
    int count = 0;
    Kid first, last;
    KidCircle(int n) {
    for(int i=0; i<n; i++) {
    add();
    }
    } void add() {
    Kid k = new Kid();
    k.id = count;
    if(count <= 0) {
    first = k;
    last = k;
    k.left = k;
    k.right = k;
    } else {
    last.right = k;
    k.left = last;
    k.right = first;
    first.left = k;
    last = k;
    }
    count ++;
    } void delete(Kid k) {
    if(count <=0) return ;
    else if(count == 1) {
    first = last = null;
    } else {
    k.left.right = k.right;
    k.right.left = k.left;
    if(k == first) {
    first = k.right;
    } else if(k == last) {
    last = k.left;
    }
    }
    count --;
    }
    }
      

  6.   

    不需要那么复杂,用原始数组即可,不用链表插来删去的。public class Kids {    private static final int LENGTH = 500;    public static void main(String[] args) {
            int[] array = new int[LENGTH];
            for (int i = 0; i < array.length; i++) {
                array[i] = i + 1;
            }        int n = 0;
            int i = 0;
            while (getSize(array) > 1) {
                if (array[i] > 0) {
                    n++;
                }
                if ((n % 3) == 0) {
                    array[i] = -1;
                }
                if (i < LENGTH - 1) {
                    i++;
                } else {
                    i = 0;
                }
            }        for (int k : array) {
                if (k > 0) {
                    System.out.println("the number of the last kid is:" + i);
                }
            }
        }    public static int getSize(int[] array) {
            int n = 0;
            for (int i : array) {
                if (i > 0) {
                    n++;
                }
            }
            return n;
        }
    }结果是268.
      

  7.   

    public class Test{  
    public static void main(String[] args){
    //创建1-500个小朋友编号
    int stuNumber=500;
    int[] childs=new int [stuNumber];
    for(int i=0;i<childs.length;i++){
    childs[i]=i+1;
    }
    //统计数到三
    int iNum=0;
    //统计结束标记
    int stopNumber=0;
    while(true){
    stopNumber=0;
    for(int i=0;i<childs.length;i++){
    if(childs[i]!=-1){
    iNum++;
    if(iNum==3){
    //判断数到了3的小朋友编号都设置为-1
    childs[i]=-1;
    iNum=0;
    }
    }else{
    stopNumber++;
    }

    }

    //当这些小朋友里面只有一个不是-1的时候结束
    if(stopNumber==childs.length-1){
    for(int i=0;i<childs.length;i++){
    if(childs[i]!=-1){
    System.out.println("这个学生的号码是"+childs[i]);
    }
    }
    break;
    }

    }
    }
    }结果是 436哦    
      

  8.   

    用队列做
    把报到1,2的人从队列前端移除并添加到原队列末尾,进行下一次报数。
    报到3的人直接从队列中移除。
    直到队列长度为1
    C#代码,java的类似 static int GetNum(int n)
            {
                Queue<int> queue = new Queue<int>();
                for (int i = 1; i <= n; ++i) queue.Enqueue(i);
                int num = 1;
                while (queue.Count > 1)
                {
                    if (num++ % 3 == 0) queue.Dequeue();
                    else queue.Enqueue(queue.Dequeue());
                }
                return queue.Dequeue();
            }