我正试图把约瑟夫斯算法写成一个学校的项目,但是我认为有一个比我已经拥有的更好的方法。
public static int[] StartTheGame(int prisoners, int count)
{
var position = count - 1;
int[] list = new int[prisoners];
for (int i = 0; i < prisoners; i++)
{
list[i] = 1;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" {0} ", list[i]);
}
Console.WriteLine();
for (int i = 1; i <= prisoners - 1; i++)
{
while (position + 1 >= prisoners)
{
position = (position) - prisoners;
}
if (list[position + 1] == 0)
{
position++;
}
if (list[position + 1] == 1)
{
list[position + 1] = 0;
}
position = position + count + 1;
ShowInConsole(prisoners, list);
}
return list;
}有什么办法让它变得更好吗?
10和2的产出是:

完整代码:
private static int _prisoners, _count;
static void Main()
{
while (true)
{
Console.ForegroundColor = ConsoleColor.White;
GetInitialValues();
int[] lastManAlive = StartTheGame(_prisoners, _count);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("The last person who is freed is standing at position {0}. ", IsFreed(lastManAlive));
}
}
private static void GetInitialValues()
{
Console.WriteLine("Please enter the number of prisoners that are going to be hanged: ");
try
{
_prisoners = (int) Convert.ToInt64(Console.ReadLine());
}
catch (Exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Something went wrong, please try again");
GetInitialValues();
}
Console.WriteLine("Please enter a number less than the number of prisoners and greater than 0 to start the game and hang the prisoners: ");
try
{
_count = (int)Convert.ToInt64(Console.ReadLine());
}
catch (Exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Something went wrong, please try again");
GetInitialValues();
}
while (_count <= 0)
{
Console.WriteLine("Please enter a number greater than 0 to start the game and hang the prisoners: ");
_count = (int)Convert.ToInt64(Console.ReadLine());
}
}
public static int[] StartTheGame(int prisoners, int count)
{
var position = count - 1;
int[] list = new int[prisoners];
InitiateGame(list, prisoners);
for (int i = 1; i <= prisoners - 1; i++)
{
while (position + 1 >= prisoners)
{
position = (position) - prisoners;
}
if (list[position + 1] == 0)
{
position++;
}
if (list[position + 1] == 1)
{
list[position + 1] = 0;
}
position = position + count + 1;
ShowInConsole(prisoners, list);
}
return list;
}
private static void InitiateGame(int[] list, int prisoners)
{
for (int i = 0; i < prisoners; i++)
{
list[i] = 1;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" {0} ", list[i]);
}
Console.WriteLine();
}
public static int IsFreed(int[] list)
{
var length = list.Length;
for (int i = 1; i <= length; i++)
{
if (list[i - 1] == 1)
{
return i;
}
}
return -1;
}
public static void ShowInConsole(int prisoners, int[] list)
{
Console.WriteLine();
for (int j = 0; j < prisoners; j++)
{
if (list[j] == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
}
Console.Write(" {0} ", list[j]);
}
Console.WriteLine();
}发布于 2016-11-30 20:29:02
在当前编写代码时,为仍活着的囚犯保存一个int令牌,对于每个步骤,请执行以下操作:
position[(index+=steps+1)%position.length] = 0;重复,直到1站着,或者没有人在prisoner_count的台阶上死亡。
(你不应该跳过身体吗?在示例输出中,在最后4行中,您只是按顺序执行其余的囚犯。我现在不能运行它,但是如果这是10和2的输出,那么10和1应该进入一个无限循环)
https://codereview.stackexchange.com/questions/148577
复制相似问题