• 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;
        }
    }
  • 相关阅读:
    树上路径(树链剖分)
    P4178 Tree(点分治)
    P2146 [NOI2015] 软件包管理器(树链剖分)
    P1903 [国家集训队]数颜色 / 维护队列 (带修莫队)
    poj1182 食物链(带权并查集)
    poj3026 Borg Maze(bfs+prim)
    最佳牛围栏(二分)
    串(dp)
    POJ1258 Agri-Net
    POJ2031 Building a Space Station(prim)
  • 原文地址:https://www.cnblogs.com/qixue/p/2515360.html
Copyright © 2020-2023  润新知