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;
        }
    }
  • 相关阅读:
    也八卦一把:李开复离开微软,投奔Google
    用于苹果OS Ⅹ Dashboard Widgets 的Google Map小部件
    下一代Hotmail和MSN Messenger最新界面截图
    Go2Map也开放了地图API
    《Excel与VBA程序设计》第三章及附录
    非广告: 365key的好处(随时收集)
    VS2005多线程程序在IDE下调试的一个问题
    买了《.net模式--架构、设计和过程》
    Expect 在网络管理中发挥着重要作用
    source insight 快捷键
  • 原文地址:https://www.cnblogs.com/qixue/p/2515360.html
Copyright © 2011-2022 走看看