首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#中使用阵列的约瑟夫斯算法

C#中使用阵列的约瑟夫斯算法
EN

Code Review用户
提问于 2016-11-30 17:58:52
回答 1查看 2K关注 0票数 7

我正试图把约瑟夫斯算法写成一个学校的项目,但是我认为有一个比我已经拥有的更好的方法。

代码语言:javascript
复制
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的产出是:

完整代码:

代码语言:javascript
复制
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();
    }
EN

回答 1

Code Review用户

发布于 2016-11-30 20:29:02

在当前编写代码时,为仍活着的囚犯保存一个int令牌,对于每个步骤,请执行以下操作:

代码语言:javascript
复制
position[(index+=steps+1)%position.length] = 0;

重复,直到1站着,或者没有人在prisoner_count的台阶上死亡。

(你不应该跳过身体吗?在示例输出中,在最后4行中,您只是按顺序执行其余的囚犯。我现在不能运行它,但是如果这是10和2的输出,那么10和1应该进入一个无限循环)

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/148577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档