当前位置:编程学习 > C#/ASP.NET >>

约瑟夫问题

今天没事用C#写了一段约瑟夫问题的代码,写完了总觉得我多用了一个变量,请达人看看是不是可以删掉一个变量。
另外,我觉得这代码写得有点长了,看看是不是哪里写的烦了?

using System;
using System.Collections.Generic;
using System.Text;

namespace Graph
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入游戏参与人数N,按回车键结束输入。");
            int N = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入游戏报数时使用的数字M,按回车键结束输入。");
            int M = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入游戏开始位置S,按回车键结束输入。");
            int S = int.Parse(Console.ReadLine());
            Console.WriteLine("");

            JosephProblem(N, M, S);

            Console.ReadLine();
        }

        static void JosephProblem(int iTotalNum, int iOutNum, int iStartNum)
        {
            // the detail info of all the parameters passed in:
            // iTotalNum, the number of all the person joined in the game
            // iOutNum, the number which is used to kick out the person
            // iStartNum, the start position of the game

            int i = 1;
            int iRemainNum = iTotalNum;// 计数N至0
            int iCount = 1;// 计数1至M


            // define an array to store the init number of every person
            int[] iAryRemainNum = new int[iTotalNum];
            for (; i <= iTotalNum; i++)
            {
                iAryRemainNum[i - 1] = i;
            }

            // if the start number is larger than the total number, define the start number as the first number
            if (iStartNum > iTotalNum)
            {
                iStartNum = 1;
            }

            // 如果数组中还有人的话
            while (iRemainNum != 0)
            {
                // 从1开始报数
                for (; iCount <= iOutNum; )
                {
                    // 如果现在这个位置还有人
                    if (iAryRemainNum[iStartNum - 1] != 0)
                    {
                        // 如果是M,从1开始继续报数
                        if (iCount == iOutNum)
                        {
                            // 踢出队列,并输出
                            Console.WriteLine(iAryRemainNum[iStartNum - 1]);
                            iAryRemainNum[iStartNum - 1] = 0;
                            iRemainNum--;
                            iCount = 1;
                            // 如果已经是最后一位了,移动到第一位
                            if (iStartNum == iTotalNum)
                            {
                                iStartNum = 1;
                            }
                            // 如果不是最后一位,指向下一位
                            else
                            {
                                iStartNum++;
                            }
                            if (iRemainNum == 0)
                            {
                                return;
                            }
                        }
                        // 如果不是M,位置移动一位到有效位置,继续报数
                        else
                        {
                            do
                            {
                                // 如果已经是最后一位了,移动到第一位
                                if (iStartNum == iTotalNum)
                                {
                                    iStartNum = 1;
                                }
                                // 如果不是最后一位,指向下一位
                                else
                                {
                                    iStartNum++;
                                }
                            } while (iAryRemainNum[iStartNum - 1] == 0);
                            // 报数
                            iCount++;
                        }
                    }
                    // 如果现在这个位置没有人了
                    else
                    {
                        // 向右移动一位
                        do
                        {
                            // 如果已经是最后一位了,移动到第一位
                            if (iStartNum == iTotalNum)
                            {
                                iStartNum = 1;
                            }
                            // 如果不是最后一位,指向下一位
                            else
                            {
                                iStartNum++;
                            }
                        } while (iAryRemainNum[iStartNum - 1] == 0);
                    }
                }
            }
        }
    }
}
约瑟夫 Joseph
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,