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;
        }
    }
  • 相关阅读:
    Vue 项目结构介绍
    使用命令行创建 Vue 项目
    GitHub无法访问怎么办?-- 已解决
    Spa 单页面应用简介
    JetBrains WebStorm 常用快捷键总结
    使用 WebStorm + Vue 写一个九九乘法表
    使用 WebStorm 2018 运行第一个 Vue 程序
    小工具
    elasticsearch安装部署
    命令行连接ftp
  • 原文地址:https://www.cnblogs.com/qixue/p/2515360.html
Copyright © 2011-2022 走看看