zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    【XSY2534】【CF835D】Palindromic characteristics 回文自动机
    启发式合并&线段树合并/分裂&treap合并&splay合并
    【XSY2534】【BZOJ4817】树点涂色 LCT 倍增 线段树 dfs序
    线性求逆元
    l1 和l2范数的真实意义
    方向导数及梯度
    大厂实习总结和反思
    高考报考以及心态调整健康贴士
    【骑士走棋盘】
    【老鼠走迷宫一】
  • 原文地址:https://www.cnblogs.com/qixue/p/2515360.html
Copyright © 2011-2022 走看看