約瑟夫問題棧

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   ext   

15個教徒與15個非教徒在深海遇險,必須將一半的人投入大海,其餘的人才能倖免於難,於是想到一個方法,30個人圍成一圈,從第一個人開始依次報數,每數到第九個人就將他扔入大海,如此迴圈直到餘15個人為止,問怎麼樣排法,才能使每次投入大海的都是非教徒?

 

 

 

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5{    class CirculLinkedList    {        //元素個數        private int count;        //尾指標        private Node tail;        //當前節點的前節點        private Node CurrPrev;        //增加元素        public void Add(object value)        {            Node newNode = new Node(value);            if (tail==null)            {//鏈表為空白                tail = newNode;                tail.next = newNode;                CurrPrev = newNode;            }            else            {//鏈表不為空白,把元素增加到鏈表結尾                newNode.next = tail.next;                tail.next = newNode;                if (CurrPrev==tail)                {                    CurrPrev = newNode;                }                tail = newNode;            }            count++;        }        //刪除當前元素        public void RemoveCurrNode()        {            //為null代表為空白表            if (tail == null)             {                throw new NullReferenceException("集合中沒有任何元素!");            }            else if (count==1)            {                tail = null;                CurrPrev = null;            }            else            {                if (CurrPrev.next==tail)                {                    tail = CurrPrev;                }                CurrPrev.next = CurrPrev.next.next;            }            count--;        }        //當前節點移動步數        public void Move(int step)        {            if (step<0)            {                throw new ArgumentOutOfRangeException("step", "移動步數不可為負數!");            }            if (tail==null)            {                throw new NullReferenceException("鏈表為空白!");            }            for (int i = 0; i < step; i++)            {                CurrPrev = CurrPrev.next;            }        }        //獲得當前節點        public object Current        {            get {return CurrPrev.next.item ;}        }        public override string ToString()        {            if (tail==null)            {                return string.Empty;            }            string s = "";            Node temp = tail.next;            for (int i = 0; i < count; i++)            {                s += temp.ToString() + "    ";                temp = temp.next;            }            return s;        }        public int Count        {            get {return count ;}        }        private   class Node        {            public Node(object  value)            {                item = value;            }            //放置資料            public object item;            public CirculLinkedList.Node next;            public override string ToString()            {                return item.ToString();            }        }    }}

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5{    class Program    {        static void Main(string[] args)        {            CirculLinkedList lst = new CirculLinkedList();            string s = string.Empty;            Console.Write("請輸入總數:");            int count = Convert.ToInt32(Console.ReadLine());            Console.WriteLine("請輸入每次要報到幾!");            int m = Convert.ToInt32(Console.ReadLine());                        Console.WriteLine("報數開始~");            for (int i = 1; i <= count; i++)            {//構建迴圈列表                lst.Add(i);            }            Console.WriteLine(lst.ToString());            while (lst.Count>15)            {                lst.Move(m);                s += lst.Current.ToString() + "      ";                lst.RemoveCurrNode();//把報數的人扔入大海                           //  Console.WriteLine("剩餘的人為: "+lst.ToString());                Console.WriteLine(lst.Current + "開始報數!");                           }            Console.WriteLine("被扔入大海的人為:"+s+lst.Current);            Console.ReadLine();        }    }}

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.