• C++ 好玩的约瑟夫环(单链表版本)


    【题目描述】

    M个人,编号分别为1到M,玩约瑟夫环游戏,最初时按编号顺序排成队列;每遍游戏开始时,有一个正整数报数密码N,队列中人依次围坐成一圈,从队首的人开始报数,报到N的人出列,然后再从出列的下一人开始重新报数,报到N的人出列;重复这一过程,直至所有人出列,完成一遍游戏,所有出列的人形成新队列;游戏可能玩很多遍,每遍有新报数密码。求若干遍游戏完成后队列次序。

    【输入描述】

    每个测试用例包含若干个正整数(至少1个),第一个正整数为玩游戏人数M,后续每个正整数为每遍游戏报数密码;报数密码可能为1。

    【输出描述】

    每个测试用例结果占一行,每个编号占4位。

    【样例输入】

    10   3   5    2
    【样例输出】
    4   6   5   2   9   1   3   7   8  10

    【完整代码示例】仅供参考

    1. #include
    2. using namespace std;
    3. class CJosephRing
    4. {
    5. struct Node
    6. {
    7. int data;
    8. Node *next;
    9. }*m_pLast;
    10. public :
    11. CJosephRing(); //构造空环
    12. void Display() const;//显示环
    13. ~CJosephRing()
    14. {
    15. _Clear ();//环清空
    16. }
    17. void Append(int x); //环队列后添加x
    18. void RunGame(int N, CJosephRing &result); //一次报数N游戏,结果环队列保存在result中
    19. private :
    20. void _Clear (); //环清空
    21. };
    22. CJosephRing::CJosephRing()
    23. {
    24. m_pLast = NULL;
    25. }
    26. void CJosephRing::Display() const
    27. {
    28. if (m_pLast == NULL)
    29. return;
    30. for (Node *p = m_pLast->next; ;p = p->next)
    31. {
    32. cout.width(4);
    33. cout<data;
    34. if (p == m_pLast)
    35. break;
    36. }
    37. cout << endl;
    38. }
    39. void CJosephRing::_Clear()
    40. {
    41. if(m_pLast == NULL)
    42. return;
    43. while (m_pLast->next != m_pLast)
    44. {
    45. Node *p = m_pLast->next;
    46. m_pLast->next = p->next;
    47. delete p;
    48. }
    49. delete m_pLast;
    50. }
    51. void CJosephRing::Append(int x)
    52. {
    53. Node *p = new Node;
    54. p->data = x;
    55. if (m_pLast == NULL)
    56. {
    57. p->next = p;
    58. m_pLast = p;
    59. return;
    60. }
    61. p->next=m_pLast->next;
    62. m_pLast->next=p;
    63. m_pLast=p;
    64. }
    65. void CJosephRing::RunGame(int N, CJosephRing &result)
    66. {
    67. if (m_pLast == NULL)
    68. return;
    69. while (m_pLast->next != m_pLast)
    70. {
    71. for (int i = 1; i < N; i++)
    72. m_pLast = m_pLast->next;
    73. Node *p = m_pLast->next;
    74. m_pLast ->next = p->next;
    75. result.Append(p->data);
    76. delete p;
    77. }
    78. result.Append(m_pLast->data);
    79. delete m_pLast;
    80. m_pLast = NULL;
    81. }
    82. int main()
    83. {
    84. int M,N;
    85. int times=0;
    86. cin>>M;
    87. CJosephRing firstRing, secondRing;
    88. for(int i=1;i<=M;i++)
    89. {
    90. firstRing.Append(i);
    91. }
    92. while (cin >>N)
    93. {
    94. if (times % 2 == 0)
    95. firstRing.RunGame(N, secondRing);
    96. else
    97. secondRing.RunGame(N, firstRing);
    98. ++times;
    99. }
    100. if (times % 2 == 0)
    101. firstRing.Display();
    102. else
    103. secondRing.Display();
    104. return 0;
    105. }

  • 相关阅读:
    什么是护网?护网怎么参加?
    自动控制原理7.5---离散系统的稳定性与稳态误差
    wifi指纹室内定位系统 计算机竞赛
    【Node.JS 】创建基本的web服务器
    基于深度学习的智能PCB板缺陷检测系统(Python+清新界面+数据集)
    [SSTF 2022] 三星安全论坛的小比赛错过了
    java计算机毕业设计springboot+vue航空公司电子售票系统-机票预订系统
    NoSQL数据库使用场景以及架构介绍
    rust智能指针
    day03 Spring-AOP面向切面编程
  • 原文地址:https://blog.csdn.net/weixin_74287172/article/details/134493811