package\u0020package2;\nimport\u0020java.util.Scanner;\nclass\u0020Node\n{\n\u0020\u0020int\u0020data;\n\u0020\u0020Node\u0020next;\n\n\u0020\u0020Node(int\u0020data)\u0020\n\u0020\u0020{\n\u0020\u0020\u0020\u0020this.data\u0020=\u0020data;\n\u0020\u0020\u0020} \n}\npublic\u0020class\u0020JosephusProblem\u0020\n{\n\u0020\u0020private\u0020static\u0020Scanner\u0020scanner;\n\tprivate\u0020static\u0020int\u0020p;\n\n\tpublic\u0020static\u0020void\u0020main(String[]\u0020args)\u0020\n\t{\n\u0020\u0020\u0020\u0020scanner\u0020=\u0020new\u0020Scanner(System.in);\n\u0020\u0020\u0020\u0020System.out.print("请输入总人数n:");\n\u0020\u0020\u0020\u0020int\u0020n\u0020=\u0020scanner.nextInt();\n\u0020\u0020\u0020\u0020System.out.print("请输入报数m:");\n\u0020\u0020\u0020\u0020int\u0020m\u0020=\u0020scanner.nextInt();\n\u0020\u0020\u0020\u0020System.out.print("请输入最终剩余人数p:");\n\u0020\u0020\u0020\u0020setP(scanner.nextInt());\n\n\u0020\u0020\u0020\u0020Node\u0020head\u0020=\u0020createJosephusCircle(n);\n\u0020\u0020\u0020\u0020Node\u0020current\u0020=\u0020head;\n\u0020\u0020\u0020\u0020while\u0020(current.next\u0020!=\u0020current)\u0020\n\u0020\u0020\u0020\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020for\u0020(int\u0020i\u0020=\u00201;\u0020i\u0020<\u0020m\u0020-\u00201;\u0020i++)\u0020\n\u0020\u0020\u0020\u0020\u0020\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020current\u0020=\u0020current.next;\n\u0020\u0020\u0020\u0020\u0020\u0020} \n\u0020\u0020\u0020\u0020\u0020\u0020System.out.print(current.next.data\u0020+\u0020" ");\n\u0020\u0020\u0020\u0020\u0020\u0020current.next\u0020=\u0020current.next.next;\n\u0020\u0020\u0020\u0020\u0020\u0020current\u0020=\u0020current.next;\n\u0020\u0020\u0020\u0020} \n\u0020\u0020\u0020\u0020System.out.println(current.data);\n\u0020\u0020\u0020} \n\n\u0020\u0020private\u0020static\u0020Node\u0020createJosephusCircle(int\u0020n)\u0020\n\u0020\u0020{\n\u0020\u0020\u0020\u0020Node\u0020head\u0020=\u0020new\u0020Node(1);\n\u0020\u0020\u0020\u0020Node\u0020current\u0020=\u0020head;\n\u0020\u0020\u0020\u0020for\u0020(int\u0020i\u0020=\u00202;\u0020i\u0020<=\u0020n;\u0020i++)\u0020\n\u0020\u0020\u0020{\n\u0020\u0020\u0020\u0020\u0020\u0020current.next\u0020=\u0020new\u0020Node(i);\n\u0020\u0020\u0020\u0020\u0020\u0020current\u0020=\u0020current.next;\n\u0020\u0020\u0020\u0020} \n\u0020\u0020\u0020\u0020current.next\u0020=\u0020head;\n\u0020\u0020\u0020\u0020return\u0020head;\n\u0020\u0020\u0020} \n\n\tpublic\u0020static\u0020int\u0020getP()\u0020\n\t{\n\t\u0020\u0020return\u0020p;\n\t}\n\n\tpublic\u0020static\u0020void\u0020setP(int\u0020p)\u0020\n\t{\n\t\u0020\u0020JosephusProblem.p\u0020=\u0020p;\n\t}\n}在数据结构中采用什么物理结构为什么内容:在这个程序中,采用的数据结构是循环链表。循环链表是一种链表的扩展形式,它的最后一个节点指向第一个节点,形成一个环形结构。\n\n在解决Josephus问题时,使用循环链表可以很方便地模拟人们围成一圈的情景。每一个节点表示一个人,节点的data属性存储了人的编号。通过不断移动指针,可以模拟报数的过程,并根据报数的规则,将指定节点从链表中移除。\n\n循环链表的优势在于,当需要删除节点时,只需要修改前一个节点的指针,而无需对整个链表进行重新连接。这样可以大大提高删除节点的效率。\n\n因此,循环链表是一种适合模拟环形问题的物理结构。

约瑟夫问题解法:循环链表的应用 - Java 代码实现

原文地址: https://www.cveoy.top/t/topic/pqrb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录