
C# 猴子选大王的算法...错在哪儿了呢?
这个算法的内容是:一圈猴子站成一圈,总数为M个,现在在圈里标示一个起始位置为1,然后从1这个猴子开始报数,报到N的猴子出列,N+1只猴子又从1开始往下报..最后只有一只猴...
这个算法的内容是:一圈猴子站成一圈,总数为M个,现在在圈里标示一个起始位置为1,然后从1这个猴子开始报数,报到N的猴子出列,N+1只猴子又从1开始往下报..最后只有一只猴子,它是王,比如有3只猴子,数到2的出列,那么第一轮是第2只出列,第3只报1,第一只报2,最后剩下第3的这只猴子,请问用C#代码怎么写这个算法呢?要具体代码,谢谢!
错误示范如下:
using System;
using System.Collections.Generic;
class s
{
static void Main()
{
int M = 3;//表示猴子总数.
int N = 2;//表示数到几猴子出列.
List<int> monkeys = new List<int>();
for (int i = 1; i <= M; i++)
{
monkeys.Add(i);
}
int currentIndex = 0;
while (true)
{
for (int i = 1; i <= N; i++)
{
Console.WriteLine("i="+i);
Console.WriteLine("currentIndex=" + currentIndex);
if (i == N)
{
Console.WriteLine("currentIndex:" + currentIndex.ToString());
Console.WriteLine("del:" + monkeys[currentIndex].ToString());
monkeys.RemoveAt(currentIndex);
if (monkeys.Count == 1)
{
Console.WriteLine(monkeys[0]);
return;
}
}
currentIndex++;
Console.WriteLine("currentIndex++=:" + currentIndex.ToString());
Console.WriteLine("monkeys.Count:" + monkeys.Count.ToString());
if (currentIndex >= monkeys.Count)
{
Console.WriteLine("currentIndex>=monkeys.Count!");
currentIndex = 0;
Console.WriteLine("currentIndex:" + currentIndex.ToString());
}
}
}
}
} 展开
错误示范如下:
using System;
using System.Collections.Generic;
class s
{
static void Main()
{
int M = 3;//表示猴子总数.
int N = 2;//表示数到几猴子出列.
List<int> monkeys = new List<int>();
for (int i = 1; i <= M; i++)
{
monkeys.Add(i);
}
int currentIndex = 0;
while (true)
{
for (int i = 1; i <= N; i++)
{
Console.WriteLine("i="+i);
Console.WriteLine("currentIndex=" + currentIndex);
if (i == N)
{
Console.WriteLine("currentIndex:" + currentIndex.ToString());
Console.WriteLine("del:" + monkeys[currentIndex].ToString());
monkeys.RemoveAt(currentIndex);
if (monkeys.Count == 1)
{
Console.WriteLine(monkeys[0]);
return;
}
}
currentIndex++;
Console.WriteLine("currentIndex++=:" + currentIndex.ToString());
Console.WriteLine("monkeys.Count:" + monkeys.Count.ToString());
if (currentIndex >= monkeys.Count)
{
Console.WriteLine("currentIndex>=monkeys.Count!");
currentIndex = 0;
Console.WriteLine("currentIndex:" + currentIndex.ToString());
}
}
}
}
} 展开
展开全部
好像挺有意思的题目,我做做看。
-----------------------------------------
还真是花了一些时间,才发现你的代码的问题在哪。主要出在currentIndex++;计数这里。当del掉一项时,currentIndex不需要++了,因为下一项的index会-1,即等于currentIndex的值。所以,修改后的代码如下:
private void MonkeyToKing()
{
int M = int.Parse(textBox1.Text);//表示猴子总数.
int N = int.Parse(textBox2.Text);//表示数到几猴子出列.
List<int> monkeys = new List<int>();
for (int i = 1; i <= M; i++)
{
monkeys.Add(i);
}
int currentIndex = 0;
while (true)
{
for (int i = 1; i <= N; i++)
{
if (i == N)
{
Console.WriteLine("del:" + monkeys[currentIndex].ToString());
monkeys.RemoveAt(currentIndex);
if (monkeys.Count == 1)
{
Console.WriteLine(monkeys[0]);
return;
}
}
else
{
currentIndex++;
}
if (currentIndex >= monkeys.Count)
{
currentIndex = 0;
}
}
}
}
M和N的取值那里的代码我修改了一下,便于测试其他数值的情况。
-----------------------------------------
还真是花了一些时间,才发现你的代码的问题在哪。主要出在currentIndex++;计数这里。当del掉一项时,currentIndex不需要++了,因为下一项的index会-1,即等于currentIndex的值。所以,修改后的代码如下:
private void MonkeyToKing()
{
int M = int.Parse(textBox1.Text);//表示猴子总数.
int N = int.Parse(textBox2.Text);//表示数到几猴子出列.
List<int> monkeys = new List<int>();
for (int i = 1; i <= M; i++)
{
monkeys.Add(i);
}
int currentIndex = 0;
while (true)
{
for (int i = 1; i <= N; i++)
{
if (i == N)
{
Console.WriteLine("del:" + monkeys[currentIndex].ToString());
monkeys.RemoveAt(currentIndex);
if (monkeys.Count == 1)
{
Console.WriteLine(monkeys[0]);
return;
}
}
else
{
currentIndex++;
}
if (currentIndex >= monkeys.Count)
{
currentIndex = 0;
}
}
}
}
M和N的取值那里的代码我修改了一下,便于测试其他数值的情况。
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询