n只猴子选大王,方法如下:按照1,2,3....n给猴子编号,然后按编号顺序坐成1圈,从1号猴子开始按编号顺序报数至m,报到m的猴子退出圈外,退出的猴子的下一只猴子重新从1开始报数至m,报到m的猴子退出,如此循环直至剩下一只猴子就是大王。要求编写一程序,n和m都是入参,返回最后一只猴子的编号,用Java或JavaScript都可以。
解决方案 »
- 求教高手关于正则表达式实现定位查找和文字计数统计问题(纯正则表达式,非程序实现)
- java将txt数据导入到excel的方法*(最好有个实例程序)
- 新手菜鸟 关于三目运算
- 求程序运行结果
- 求设置http代理的代码怎么写啊?
- numIncre(“000099”) => “000100”,返回同闊度數字型字串,请教大家有什么更好的实现方法?
- 问个简单的问题<td><%=rs.getDouble("STANDARD_VALUE")/rs.getDouble("ACTUAL_VALUE")%>倍</td>为什么不行
- 为什么我生成的.jar文件无法运行?
- 类型转换问题?
- 再次请朋友们帮忙了,进来都有分
- 关于ScheduledThreadPoolExecutor的想法
- 我现在这有个关于linux的问题想向大家求教一下~
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());
}
}