josephus problem——约瑟夫问题

无意间听到有这么个问题,于是第一想法就是用环形链表解决

约瑟夫斯问题(有时也称为约瑟夫斯置换),是一个出现在计算机科学数学中的问题。在计算机编程的算法中,类似问题又称为约瑟夫环

据说著名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。然而Josephus 和他的朋友并不想遵从。

有N个囚犯站成一个圆圈,准备处决。首先从一个人开始,越过k-2个人(因为第一个人已经被越过),并杀掉第k个人。接着,再越过k-1个人,并杀掉第k个人。这个过程沿着圆圈一直进行,直到最终只剩下一个人留下,这个人就可以继续活着。

问题是,给定了N和K,一开始要站在什么地方才能避免被处决?

给出我的解:

/*******************************************************************
Code Writer: EOF
Date : 2014.02.14

Purpose:
	In computer science and mathematics, the Josephus Problem (or Josephus permutation) 
	is a theoretical problem related to a certain counting-out game.
	
	There are people standing in a circle waiting to be executed. 
	The counting out begins at some point in the circle and proceeds
	around the circle in a fixed direction. In each step, a certain 
	number of people are skipped and the next person is executed. 
	The elimination proceeds around the circle (which is becoming 
	smaller and smaller as the executed people are removed), until 
	only the last person remains, who is given freedom.

Touche me: If there are some problems in my codes, please touche me.
My e-mail is jasonleaster@gmail.com .It is glad to know you.

********************************************************************/

#include<stdio.h>
#include<stdlib.h>
#define MOVENUMBER 2 //We skip MOVENUMBER people every time

struct people
{
	int location;
	struct people* next;
}*header;

struct people* creat_circle_list(const int ListSize);//Creat a circle-list for modelling people in the game
void free_circle_list_t(struct people* const header,const int ListSize);//free the memory we allocated
int game(struct people* const p_firt_man,int ListSize);//modelling the game.

int main()
{
	int number_of_people = 0;
	printf("Please enter the number of people in the room\n");
	while(!scanf("%d",&number_of_people))
	{
		printf("scanf error!\nPlease enter again!\n");
	}

	if(creat_circle_list(number_of_people) == NULL)
	{
		printf("The application of creating a circle list is wrong!\nProcess End!\n");
		free_circle_list_t(header,number_of_people);
		return 0;
	}
	else
	{
		game(header,number_of_people);
	}

	return 0;
}

struct people* creat_circle_list(const int ListSize)
{
	int temp = 0;
	struct  people* p_now    = NULL;//pointer to current node 
	struct  people* p_before = NULL;//pointer to before node
	header = (struct people*)malloc(sizeof(struct people));
	header->location = 0;//initialize header's loaction
	p_before = header;
	for(temp = 0;temp < ListSize-1;temp++)
	{
		p_now = (struct people*)malloc(sizeof(struct people));
		p_now->location = temp+1;
		if(p_now == NULL)//If memory allocation is failed, end the function
		{
			return NULL;
		}
		p_before->next = p_now;
		p_before = p_now;
	}
	p_now->next = header;//link it as a circle

	return header;
}

void free_circle_list_t(struct people* const header,const int ListSize)
{
	int temp = 0;
	struct people* p_now	 = header;
	struct people* p_before = NULL;
	struct people* p_next 	 = NULL;
	struct people* p_free_people = NULL;
	for(temp =0;temp< ListSize;temp++)
	{
		p_free_people = p_now->next ;
		p_now->next = p_now->next->next;
		free(p_free_people);
	}
}

int game(struct people* const p_first_man,int ListSize)
{
	int counter = 0;
	int num = 0;
	struct people* p_temp = NULL;
	struct people* p_before = p_first_man;
	for (counter = 0;counter < ListSize-1; counter++)
	{
		for(num = 0,p_temp = p_before;num < MOVENUMBER;num++)//move three people
		{
			p_before = p_temp;
			p_temp = p_temp->next;
		}
		
		if(p_temp == header)
		{
			header = p_temp->next;
		}
		p_before->next = p_temp->next;
		free(p_temp);//the people move out of the circle
		}
		printf("The last people lefted is %d\n",header->loaction);
	
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值