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;
        }
    }
  • 相关阅读:
    转 Hystrix超时实现机制
    Hystrix实现ThreadLocal上下文的传递 转
    MongoDB里做表间关联
    转载:mongodb的两阶段提交实战
    MongoDB中的读写锁
    Mongodb中的 原子性 隔离性
    web服务优化
    MongoDB分片(Sharding)技术
    MongoDB复制集
    consul分布式集群搭建
  • 原文地址:https://www.cnblogs.com/qixue/p/2515360.html
Copyright © 2011-2022 走看看