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!
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()); } }
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 的退出
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!
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());
}
}