Josephus 问题

Josephus Problem C#实现:

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

namespace Josephus
{
    class Program
    {
        static void Main(string[] args)
        {
            JosephusOutput(-1110);
        }

        static void JosephusOutput(int M, int N)
        {
            //Input validation check
            if (M < 0 || N < 0)
                return;

            //Initialize the sequence
            JosephusData[] Jdata = new JosephusData[N];
            for (int i = 0; i < N; i++)
            {
                Jdata[i] = new JosephusData();
                Jdata[i].Value = i + 1;
                Jdata[i].IsOutLine = false;
            }

            int mark = 0;   //If all numbers are out of line, the mark equals N

            int j = 0;
            int m = M;
            while (mark != N)
            {
                if (Jdata[j].IsOutLine == false)
                {
                    m--;
                }
                //Make the number out of line
                if (m == 0)
                {
                    Jdata[j].IsOutLine = true;
                    Console.WriteLine(Jdata[j].Value);
                    mark++;
                    m = M;
                }
                j = (j + 1) % N;
            }
        }
    }
    class JosephusData
    {
        public int Value;
        public bool IsOutLine;
    }
}
原文地址:https://www.cnblogs.com/qixue/p/2515360.html