Josephus problem(约瑟夫问题,丢手绢问题)
约瑟夫问题
约瑟夫环问题是一个数学应用题:已知n个人(以编号1,2,3.....,n)围坐在一张圆桌的周围。从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列,以此规律重复下去,直到圆桌的人全部出列。通常解决这类问题时我们把编号从0-n-1,最后+1即为原问题的解。
一、算法描述:
约瑟夫环运作如下:
- 一群人围在一起坐成环状
- 从某个编号开始报数(如:K)
- 数到某个数(如:M)的时候,此人出列,下一个人重新报数
- 一直循环,直到所有人出列 ,约瑟夫环结束
二、解决此类问题的解法
C语言 模拟递归法
公式推导法
#include <stdio.h>
#include <stdlib.h>
struct _Node
{
int data;
struct _Node *next;
};
typedef struct _Node node_t;
typedef struct _Linklist
{
node_t *phead;
node_t *ptail;
int len;
} Linklist;
static node_t *GetNode(int i) //新建并初始化节点
{
node_t *pNode;
pNode = (node_t *)malloc(sizeof(node_t));
if (!pNode)
{
printf("Error,thememoryisnotenough!\n");
exit(-);
}
pNode->data = i;
pNode->next = NULL;
return pNode;
}
void init_list(Linklist *plist) //用第一个节点初始化循环单链表
{
node_t *p;
p = GetNode();
//printf("TheNewNodeis:%d\n",p->data);//****TEST****
plist->phead = p;
plist->ptail = p;
p->next = plist->phead;
plist->len = ;
}
static void Create_List(Linklist *plist, int n) //把其余数据添加到循环单链表中
{
int i = ;
node_t *pNew;
for (i = ; i <= n; i++)
{
pNew = GetNode(i);
/********TEST********
printf("TheNewNodeis:%d\n",pNew->data);
********TEST********/
plist->ptail->next = pNew;
plist->ptail = pNew;
pNew->next = plist->phead;
plist->len++;
}
printf("Completesthee-waycirculationchaintablethefoundation!\n");
}
void Print_List(Linklist *plist) //输出链表内容
{
node_t *pCur = plist->phead;
do
{
printf("The%dperson.\n", pCur->data);
pCur = pCur->next;
} while (pCur != plist->phead);
printf("ThelengthoftheList:%d\n", plist->len);
} // 约瑟夫回环函数实现 void joseph(Linklist *plist, int m) //约瑟夫回环函数实现
{
node_t *pPre = plist->ptail;
node_t *pCur = plist->phead;
int i;
while (plist->len != )
{
i = ;
while (i < m - )
{
pPre = pPre->next;
i++;
}
pCur = pPre->next;
pPre->next = pCur->next;
free(pCur);
plist->len--;
}
printf("Thelastoneis:%d\n", pPre->data);
}
int main()
{
int n = ;
printf("PleaseinputtheLengthoftheCirclelist:");
scanf("%d", &n);
int m = ;
printf("PleaseinputtheStoppoint:");
scanf("%d", &m);
Linklist pList;
init_list(&pList);
Create_List(&pList, n);
Print_List(&pList);
joseph(&pList, m);
return ;
}
三、OJ 例题
题目描述

解题思路:
1<=n<1000发现数据量不大,直接模拟游戏求出最后一个人
下面是非递归使用for循环代码
#include <stdio.h>
#include <stdlib.h> int main()
{
int n;
while (scanf_s("%d", &n) == )
{
int len, i, j=;
int count = n;//当前存在的人数 当count == 1剩下最后一个人 即为答案
int a[] = { };//初始化模拟数组
for (i = ; i < n; i++)//人数从1~n数组标示从0~n-1
{
if (count == )
break;
if (a[i] == )//a[i]==1表示这个人已经退出游戏
{
if (j % == )
{
a[i] = ;
count--;//删掉一个人
}
else
{
if (i + >= n)//数组循环即 当i往下大于人数n时需要从0开始 但是由于是for 循环i会++ 所以i置为-1
i = -;
}
j++;
}
else
{
if (i + >= n)
i = -;//同上
}
if (i == n-)
i = -;//同上
}
for (i = ; i < n; i++)
if (a[i]==)
printf("%d\n", i + );
}
}
非递归使用while循环
#include <stdio.h>
#include <stdlib.h> int main()
{
int n;
while(scanf("%d",&n)==)
{
int i=,len,a[]={};
int flag=;
len=n-;
while(len)
{
if(a[i]==)
{
if(flag==)
{
i++;
if(i>n)
i=;
flag=;
}
else
{
a[i]=;
i++;
if(i>n)
i=;
flag=;
len--;
}
}
else
{
i++;
if(i>n)
i=;
}
}
for(i=;i<=n;i++)
if(a[i]==)
printf("%d\n",i);
}
}
题目数字较大的时候可以采用公式推导法,参考下面链接的博客
https://blog.csdn.net/u011500062/article/details/72855826
Josephus problem(约瑟夫问题,丢手绢问题)的更多相关文章
- 约瑟夫问题(Josephus Problem)的两种快速递归算法
博文链接:http://haoyuanliu.github.io/2016/04/18/Josephus/ 对,我是来骗访问量的!O(∩_∩)O~~ 约瑟夫问题(Josephus Problem)也称 ...
- 算法Sedgewick第四版-第1章基础-017一约瑟夫问题(Josephus Problem)
/************************************************************************* * * Josephus problem * * ...
- Bloomberg面经准备: Josephus problem
Given a circular single linked list.Write a program that deletes every kth node until only one node ...
- josephus Problem 中级(使用数组模拟链表,提升效率)
问题描写叙述: 在<josephus Problem 0基础(使用数组)>中.我们提出了一种最简单直接的解决方式. 可是,细致审视代码之后.发现此种方案的效率并不高,详细体如今.当有人出局 ...
- Josephus Problem的详细算法及其Python、Java实现
笔者昨天看电视,偶尔看到一集讲述古罗马人与犹太人的战争--马萨达战争,深为震撼,有兴趣的同学可以移步:http://finance.ifeng.com/a/20170627/15491157_0. ...
- python玩丢手绢问题,出局的顺序
# 丢手绢问题# 游戏规则: 有N个小朋友玩丢手绢游戏,做成一圈,从第一个小朋友开始数数,从一开始数,数到指定数字的小朋友要出列,然后下一个小朋友继续从1开始数,依次类推,算出最后一个留下来的小朋友是 ...
- NC207040 丢手绢
NC207040 丢手绢 题目 题目描述 "丢丢丢手绢,轻轻地放在小朋友的后面,大家不要告诉她,快点快点抓住她,快点快点抓住她." 牛客幼儿园的小朋友们围成了一个圆圈准备玩丢手绢的 ...
- 约瑟夫(环)问题(Josephus problem)
问题描述:皇帝决定找出全国中最幸运的一个人,于是从全国选拔出 n 个很幸运的人,让这 n 个人围着圆桌进餐,可是怎么选择出其中最幸运的一个人呢?皇帝决定:从其中一个人从 1 开始报数,按顺序数到第 k ...
- LightOJ - 1179 Josephus Problem(约瑟夫环)
题目链接:https://vjudge.net/contest/28079#problem/G 题目大意:约瑟夫环问题,给你n和k(分别代表总人数和每次要数到k),求最后一个人的位置. 解题思路:因为 ...
随机推荐
- android学习笔记(9)android程序调试学习
相应若水老师的第十四课 一,Log日志输出 Log.v(tag,message); //verbose模式,打印最具体的日志 Log.d(tag,message); // ...
- Java中的equals()和hashCode()
概述 在我们使用类集框架(比方使用hashMap.hashSet)的时候,常常会涉及到重写equals()和hashCode()这两个方法. 这两个方法的联系是: 1. 假设两个对象不同,那么他们的h ...
- MongoDB集群——分片
1. 分片的结构及原理分片集群结构分布: 分片集群主要由三种组件组成:mongos,config server,shard1) MONGOS数据库集群请求的入口,所有的请求都通过mongos进行协调, ...
- 3736 【HR】万花丛中2
3736 [HR]万花丛中2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description HR神犇在成功攻略ZX后,花心 ...
- 安卓dex 文件结构简要说明
#ifndef _DEX_FILE_HELPER_ #define _DEX_FILE_HELPER_ //此文件仅仅是起帮助作用,帮助不太了解DexFile结构的了解一下DexFile相关结构,想更 ...
- git ldap
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md ldap : enabled : true host : 'ope ...
- DVB条件接收系统多密技术的设计与实现
1相关技术比较 1.1DVB同密 DVB同密技术的目的是将两家或两家以上的CA系统应用于同一网络平台中,从电视台角度实现技术的选择和竞争的环境.同密允许在传输的同一套节目流中携带由不同CAS生成的多个 ...
- Spark SQL中 RDD 转换到 DataFrame (方法二)
强调它与方法一的区别:当DataFrame的数据结构不能够被提前定义.例如:(1)记录结构已经被编码成字符串 (2) 结构在文本文件中,可能需要为不同场景分别设计属性等以上情况出现适用于以下方法.1. ...
- codeforces——贪心
codeforces 804A Find Amir http://codeforces.com/problemset/problem/804/A /* 题意:给定n个学校,需要遍历所有学校,可从任 ...
- StreamingListener技术点
以下是对StreamingListene的研究,由于比较简单,故只贴代码,不做解释 /** * Created by gabry.wu on 2016/5/27. * 实现StreamingListe ...