n只猴子选大王,方法如下:按照1,2,3....n给猴子编号,然后按编号顺序坐成1圈,从1号猴子开始按编号顺序报数至m,报到m的猴子退出圈外,退出的猴子的下一只猴子重新从1开始报数至m,报到m的猴子退出,如此循环直至剩下一只猴子就是大王。要求编写一程序,n和m都是入参,返回最后一只猴子的编号,用Java或JavaScript都可以。

解决方案 »

  1.   

    楼主    我是个初学者  ,   刚学了for循环 ,  也很想知道你这个题怎么做 ,    有了答案麻烦告诉我一下   。 谢谢!   
      

  2.   

    这个问题挺经典的,那个ACM程序设计大赛就出过这样的题。用循环单链表试试。
      

  3.   


    function Monkey(sn, next) {
    this.sn = sn;
    this.next = next;
    }function electedKing(n, m) {
    var theMonkey = null;
    var first = null;
    for (var i=1; i<=n; i++) {
    if (theMonkey == null) {
    first = theMonkey = new Monkey(i);
    } else {
    theMonkey.next = new Monkey(i);
    theMonkey = theMonkey.next;
    }
    }
    theMonkey.next = first;

    while (theMonkey.next != theMonkey) {
    for (var i=1; i<m; i++) {
    theMonkey = theMonkey.next;
    }
    theMonkey.next = theMonkey.next.next;
    }
    WScript.Echo(theMonkey.sn);
    }
    electedKing(10, 5); // 10 只猴子选大王, 报到 5 的退出
      

  4.   

    浙大ACM平台上的题目吧,猴子选大王问题。百度里搜索下,会有相应算法的。
      

  5.   


    import java.util.*;class Monkey
    {
    int index; Monkey(int i)
    {
    index = i;
    }
    }public class Monkeys
    {
    ArrayList<Monkey> monkeys = new ArrayList<Monkey>();
    public Monkeys(int nMonkeys)
    {
    for (int index =1;index<=nMonkeys;index++)
    {
    monkeys.add(new Monkey(index));
    }
    } public Monkey selectKing(int m)
    {
    int startIndex = 0; while(monkeys.size() != 1)
    {
    int removeIndex = (startIndex + m-1) % monkeys.size();
    monkeys.remove( removeIndex );
    startIndex = removeIndex;
    } return monkeys.get(0);
    } public static void main(String[] args)
    {
    int[] params = {0,0};
    if (!checkArgs(params, args)) return; Monkeys monkeys = new Monkeys(params[0]);
    System.out.println("The King of Monkeys is No."+ monkeys.selectKing(params[1]).index + "!");
    } private static boolean checkArgs(int[] result, String... args)
    {
    if (args.length < 2)
    {
    System.out.println("You must give me the number of Monkeys and max value of count by argument.");
    return false;
    }
    else if (args.length >2)
    {
    System.out.println("Two many arguments. Please pass only two arguments");
    return false;
    } int index = 0; for (String s : args)
    {
    try
    {
    int x = 0;
    try
    { x = Integer.parseInt(s);}
    catch (NumberFormatException e)
    {
    NumberFormatException e1 = new NumberFormatException(e.getMessage() 
                                                    +  "\nInput argument must be decimal digits");
    throw e1;
    } if (x<=0)
    {
    NumberFormatException e = new NumberFormatException("For input string \""+s
                                                    +"\"." + "\nArgument must be > 0.");
    throw e;
    }
    result[index++]=x;
    }
    catch (NumberFormatException e)
    {
    System.out.println("One argument is invalid. Check it!");
    System.out.println(e.getMessage());
    return false;
    }
    }
    return true;
    }
    }
    我的代码
    运行结果 
    java Monkeys 10 5
    The King of Monkeys is No.3!java Monkeys 3 2
    The King of Monkeys is No.3!java Monkeys 8 4
    The King of Monkeys is No.6!java Monkeys 20 9
    The King of Monkeys is No.6!
      

  6.   

    import java.util.*;
    public class SelectKing 
    {
    final  static int cnt=3;
    public static void main(String[] args)
    {
    int count=0;
    LinkedList<Integer> monkey = new LinkedList<Integer>();
    System.out.println("请输入猴子的总数:");
    Scanner in = new Scanner(System.in);
    Integer total = Integer.parseInt(in.nextLine());
    for(int i=1;i<=total;i++)
    {
    monkey.add(i);
    }
    Iterator iter = monkey.iterator(); //获取链表的迭代器,指向表头
    while(total>1)
    {
    if(iter.hasNext()) //判断链表下一个是否还有节点
    {
    iter.next();//取下一个节点
    count++;
    }
    else
    {
    iter = monkey.iterator();//获取链表的迭代器,指向表头
    }
    if(count==3)
    {
    count = 0;
    iter.remove();//移走当前的节点
    total--;
    }
    }
    System.out.println("monkeyKing = "+monkey.peek());
    }
    }