约瑟夫死亡游戏循环链表实现

游戏描述:

30 个人在一条船上,超载,需要 15 人下船。

于是人们排成一队,排队的位置即为他们的编号。

报数,从 1 开始,数到 9 的人下船。

如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢?

#include<stdio.h>
#include<stdlib.h>
struct shipper
{
	int num;
	struct shipper* p_num;
};
typedef struct shipper Shipper;

int main()
{

int i;
int count;
count=0;
Shipper* ship_temp;
Shipper* ship_head;
Shipper* ship_ele;
ship_temp = (Shipper *)malloc(sizeof(Shipper)); 
ship_head=ship_temp;

for(i=1;i<=30;i++)
{	
 
ship_ele = (Shipper *)malloc(sizeof(Shipper));  //malloc	
 
ship_ele->num=i;
 
ship_temp->p_num=ship_ele;  //temp先作为头节点 ,然后temp指向下一节点 
 
ship_temp=ship_ele;  
	
}
/***********链表首尾衔接**********/
ship_temp->p_num=ship_head->p_num; 
/***********验证循环链表**********/
while(0)
{
printf("%d\n",ship_head->p_num->num) ;
ship_head->p_num=ship_head->p_num->p_num; 
} 


ship_temp=ship_head->p_num;	
while(count<=14)
{
count=count+1;
for(i=1;i<=7;i++)
{
 ship_temp=ship_temp->p_num;
 
 if(i==7) 
 {
 printf("下船人员编号为:%d\n",ship_temp->p_num->num);
 ship_temp->p_num=ship_temp->p_num->p_num; 
 } 
}	
ship_temp=ship_temp->p_num;
}


		
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值