约瑟夫问题

约瑟夫环问题是一个数学应用题:已知n个人(以编号1,2,3.....,n)围坐在一张圆桌的周围。从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列,以此规律重复下去,直到圆桌的人全部出列。通常解决这类问题时我们把编号从0-n-1,最后+1即为原问题的解。

一、算法描述:

约瑟夫环运作如下:

  1. 一群人围在一起坐成环状
  2. 从某个编号开始报数(如:K)
  3. 数到某个数(如:M)的时候,此人出列,下一个人重新报数
  4. 一直循环,直到所有人出列 ,约瑟夫环结束

二、解决此类问题的解法

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

最新文章

  1. lbs(查看附近的人),看看社交软件如何实现查看附近的人
  2. POJ 1279 Art Gallery(半平面交)
  3. IGS_学习笔记06_IREP发布客户化集成接口为Web Service(案例)
  4. iPad中控制器view的width和height
  5. 踩刹车——regularization
  6. CSS的inherit与auto使用分析
  7. Hibernate 事物隔离级别 深入探究
  8. 基于jQuery仿uploadify的HTML5图片上传控件jquery.html5uploader
  9. Qt 图形特效(Graphics Effect)介绍
  10. iOS_SN_push/pop转场动画封装和一般动画封装
  11. 部署Replica Sets及查看相关配置
  12. Linux Logwatch的学习总结
  13. keepalived+lvs子网掩码造成VIP切换故障 + vrrp_script+track_script
  14. 洛谷P4332 [SHOI2014]三叉神经树(LCT,树剖,二分查找,拓扑排序)
  15. Mysql 模糊匹配(字符串str中是否包含子字符串substr)
  16. Jmeter-安装配置
  17. python验证代理IP
  18. [置顶] app后端设计--总目录
  19. LDAP summary-- Python ldap
  20. java中的块

热门文章

  1. 不折移动web不腾--开启我的个人Mac之旅
  2. 移动匿名支付购物方案 A Lightweight Anonymous Mobile Shopping Scheme Based on DAA for Trusted Mobile Platform
  3. 为部门整理的mysql_db使用军规
  4. hdu5396 Expression 区间dp +排列组合
  5. ListView布局之View复用原理举例
  6. express的路由
  7. linux设备驱动模型二【转】
  8. 机器视觉: LBP-TOP
  9. [Swift通天遁地]三、手势与图表-(12)创建复合图表:包含线性图表和柱形图表
  10. Vue解决移动端localhost无数据问题