数据结构-谜题

发布于 2024-11-09 20:57:45 字数 330 浏览 4 评论 0 原文

有 5 名成员围坐在一张桌子旁。关键值是坐在桌子周围的成员数量。所以现在关键值为5。一名恐怖分子告诉成员,由于你们有5名成员,我将从第一个成员开始数起,数到5的人将被枪杀。 他数了数,第五个人死了。他再次数到五,第一个人死了。他再次数数,第三个人死了,现在还剩下2和4。他数着他们之间的数,最后4将被算作5,他被枪杀。最后剩下的人将是2。

如果尝试的话也是一样的对于七个人来说,答案是 8。 对于 8 个人来说,答案是 4。

如何为此设置一个公式,以便计算机能够正确拍摄该人。

我猜想它可能是在一个循环链表中,通过给成员提供一个令牌值。但我无法得出一个等式。因此,通过给出关键值,将确定将生存的人。

There are 5 members sitting around a table. Key value is the number of members sitting around the table. So now the key value will be 5. A terrorist told the members that as you are 5 members I will count from first member and the person who is counted 5 will be shot dead.
He counts and the 5th person dies. Once again he counts up to five and the 1st person dies. Once again he counts and the 3rd person dies, and now 2 and 4 are remaining.He counts the no between them finally 4 will be counted as 5 and he is shot dead.The last person remaining will be 2.

Like the same if tried for seven persons the answer will be 8.
And for 8 persons the answer will be 4.

How to set a formula for this so that computer can shoot the person correctly.

I guess it might be in a circular linked list by giving a token value to the members.But i could not arrive at an equation. So by giving the key value the person who will live will be determined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦归所梦 2024-11-16 20:57:46

这是一个名为约瑟夫问题的经典问题。
它有递归解:

J (1) = 1 ;这是基础

当 n 为偶数时 J(2n) = 2J(n) - 1

当 n 为奇数时 J(2n + 1) = 2J(n) + 1

it's classic problem named Josephus problem .
it has recursive solution :

J (1) = 1 ; this is the basis

when n is even J(2n) = 2J(n) - 1

when n is odd J(2n + 1) = 2J(n) + 1

蓝海似她心 2024-11-16 20:57:45

这被称为约瑟夫问题

This is termed the Josephus problem.

清风夜微凉 2024-11-16 20:57:45

这是一个众所周知的问题,称为约瑟夫问题。检查 wikipediamathworld 寻找可能的解决方案。您可以使用 Google 查找大量有关它的文章。

This is a well known problem called Josephus problem. Check wikipedia and mathworld for possible solution. And you can use Google for numerous articles on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文