黑马程序员技术交流社区

标题: 约瑟夫环 [打印本页]

作者: 彭家贰小姐    时间: 2013-8-12 23:43
标题: 约瑟夫环
约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。

  1. using System;

  2. namespace test26
  3. {
  4. public class CircularLinkedList
  5. {
  6. private class Node
  7. {
  8. public Node(object value)
  9. {
  10. Item = value;
  11. }

  12. public readonly object Item;
  13. public Node Next;

  14. public override string ToString()
  15. {
  16. return Item.ToString();
  17. }
  18. }

  19. private int _count; //记录元素个数;

  20. public int Count
  21. {
  22. get { return _count; }
  23. }

  24. private Node _tail; //尾指针;

  25. public object Current //指示当前节点中的值;
  26. {
  27. get { return _currentPre.Next.Item; }
  28. }

  29. private Node _currentPre; //使用前驱结点来标示当前节点;

  30. //在链表尾部添加元素;
  31. public void Add(object values)
  32. {
  33. var newNode = new Node(values);
  34. if (_tail == null) //如果链表为空,则新节点既是头指针,又是尾指针;
  35. {
  36. _tail = newNode;
  37. _tail.Next = newNode;
  38. _currentPre = newNode;
  39. }
  40. else
  41. {
  42. newNode.Next = _tail.Next;
  43. _tail.Next = newNode;
  44. if (_currentPre == _tail)
  45. {
  46. _currentPre = newNode;
  47. }
  48. _tail = newNode; //把尾指针指向新节点;
  49. }
  50. _count++;
  51. }

  52. //删除当前节点;
  53. public void RemoveCurrentNode()
  54. {
  55. if (_tail == null)
  56. {
  57. throw new NullReferenceException("集合中没有任何元素!");
  58. }
  59. if (_count == 1)
  60. {
  61. _tail = null;
  62. _currentPre = null;
  63. }
  64. else
  65. {
  66. if (_currentPre.Next == _tail)
  67. {
  68. _tail = _currentPre;
  69. }
  70. _currentPre.Next = _currentPre.Next.Next;
  71. }
  72. _count--;
  73. }

  74. //让当前节点向前移动指定的步数;
  75. public void Move(int step)
  76. {
  77. if (step < 0)
  78. {
  79. throw new ArgumentOutOfRangeException("移动步数不能小于0" + "!");
  80. }
  81. if (_tail == null)
  82. {
  83. throw new NullReferenceException("集合中没有任何元素!");
  84. }
  85. for (int i = 0; i < step - 1; i++)
  86. {
  87. _currentPre = _currentPre.Next;
  88. }
  89. }

  90. //打印整个链表;(仅用于测试)
  91. public override string ToString()
  92. {
  93. if (_tail == null)
  94. {
  95. return string.Empty;
  96. }
  97. string s = "";
  98. Node temp = _tail.Next;
  99. for (int i = 0; i < _count; i++)
  100. {
  101. s += temp + " ";
  102. temp = temp.Next;
  103. }
  104. return s;
  105. }
  106. }

  107. /// <summary>
  108. /// Program类
  109. /// </summary>
  110. internal class Program
  111. {
  112. /// <summary>
  113. /// Main
  114. /// </summary>
  115. /// <param name="args"></param>
  116. private static void Main(string[] args)
  117. {
  118. //题目: 约瑟夫环

  119. var clist = new CircularLinkedList();
  120. var s = string.Empty;

  121. Console.WriteLine("请输入总人数:");
  122. var count = Convert.ToInt32(Console.ReadLine());

  123. Console.WriteLine("请输入间隔数字M的值:");
  124. var m = Convert.ToInt32(Console.ReadLine());

  125. Console.WriteLine("开始:");
  126. for (int i = 1; i < count + 1; i++)
  127. {
  128. clist.Add(i);
  129. }

  130. Console.WriteLine("所有人:" + clist + "/n");
  131. while (clist.Count > 1)
  132. {
  133. clist.Move(m);
  134. s += clist.Current + " ";
  135. Console.WriteLine("出局的人:" + clist.Current);
  136. clist.RemoveCurrentNode();
  137. Console.WriteLine("剩余的人:" + clist);
  138. Console.WriteLine("开始数数的人:" + clist.Current);
  139. }
  140. Console.WriteLine("出队顺序:" + s + clist.Current);
  141. Console.ReadKey();
  142. }
  143. }
  144. }
复制代码

作者: 咖喱猫    时间: 2013-8-12 23:49
看起来蛮高级的{:soso_e129:}
作者: 熊丽    时间: 2013-8-25 09:51
牛X丫{:soso_e179:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2