约瑟夫斯问题_什么是约瑟夫斯问题?

约瑟夫斯问题

This is an interesting programming
problem know in the literature as Josephus problem. The problem is given as:
1. Suppose there are n children standing
in a queue.
2. Students are numbered from 1 to n
in the clockwise direction.
3. Students choose a lucky number say
m.
They start counting in clockwise
direction from the child designated as 1. The counting proceeds until the mth
child is identified. mth child is eliminated from the queue.
Counting for the next round begins from the child next to the eliminated one
and proceeds until the mth child is identified. This child is then
eliminated and the process continues. After few rounds of counting only one
child is left and this child is declared as winner.
Josephus Problem Animation

Implementation

It is required to write a program
to identify a winner of the game. Inputs to the function are two parameters.
n: number of children
m: lucky number
function returns an integer
identifying the winner
int winner (int m, int n)
{
                                 queue
q;
                                 int
i;
                                 init
(&q);             //create a queue of
n integers, numbered from 1 to n. Number I stands for ith child
                                 for
(i=1; i<=n; i++)
                                                enqueue (&q, i);
                                 for
(j=1; j<=n; j++)          //n-1 iterations
to eliminate n-1 children
                                 {
                                                for
(i=1; 1<m; i++)            //skip m-1
children
                                                {
                                                                x=dequeue
(&q);
                                                                enqueue
(&q, x);
                                                }
                                                x=dequeue
(&q);             //remove mth children
                                 }
                                 x=dequeue
(&q);            //last child winner
                                 return
(x);
}
This is an interesting programming
problem know in the literature as Josephus problem. The problem is given as:
1. Suppose there are n children standing
in a queue.
2. Students are numbered from 1 to n
in the clockwise direction.
3. Students choose a lucky number say
m.
They start counting in clockwise
direction from the child designated as 1. The counting proceeds until the mth
child is identified. mth child is eliminated from the queue.
Counting for the next round begins from the child next to the eliminated one
and proceeds until the mth child is identified. This child is then
eliminated and the process continues. After few rounds of counting only one
child is left and this child is declared as winner.
Josephus Problem Animation

Implementation

It is required to write a program
to identify a winner of the game . Inputs to the function are two parameters.
n: number of children
m: lucky number
function returns an integer
identifying the winner
int winner (int m, int n)
{
                                 queue
q;
                                 int
i;
                                 init
(&q);             //create a queue of
n integers, numbered from 1 to n. Number I stands for ith child
                                 for
(i=1; i<=n; i++)
                                                enqueue (&q, i);
                                 for
(j=1; j<=n; j++)          //n-1 iterations
to eliminate n-1 children
                                 {
                                                for
(i=1; 1<m; i++)            //skip m-1
children
                                                {
                                                                x=dequeue
(&q);
                                                                enqueue
(&q, x);
                                                }
                                                x=dequeue
(&q);             //remove mth children
                                 }
                                 x=dequeue
(&q);            //last child winner
                                 return
(x);
}

翻译自: https://www.thecrazyprogrammer.com/2014/02/what-is-josephus-problem.html

约瑟夫斯问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值