约瑟夫问题的C语言求解

//assuming that there are N people  here in a circle and every Mth people is killed untill the last man.
//Then who will be alive.
//This program is aimed to solve this problem.
#include<stdio.h>
#include<stdlib.h>

typedef struct node* link;
struct node
{
	int item;
	link next;
};

main()
{
	int N;
	int M;
	int i=1;
	link t=malloc(sizeof *t);
	link x=t;
	link temp;

	scanf("%d %d",&N,&M);
	t->item=1;
	t->next=t;
	
	for(i=2;i<=N;i++)
	{
		x->next=malloc(sizeof *x);
		x=x->next;
		x->item=i;
		x->next=t;
	}                      //right now the pointer x is pointing the last one of the list and make sure of it 
	
	while(x!=x->next)
	{
		for(i=1;i<M;i++)
		{
			x=x->next;
		}
		temp=x->next;
		printf("%3d",temp->item);
		x->next=x->next->next;
		free(temp);	       //It is very important to free the one which is deleted
	}
	printf("\n%d\n",x->item);
}
//下面是用链表实现约瑟夫问题的方法:
//main()

#include<stdio.h>
#include<stdlib.h>
#include"head.h"


main()
{
	int i;
	int N=9;
	int M=5;
	link temp;
	link x=initial_circle(N);
	while(x!=x->next)
	{
		for(i=1;i<M-1;i++)
		{
			x=x->next;
		}
		temp=x->next;
		x->next=x->next->next;
		free(temp);
		x=x->next;
	}
	printf("%d",x->item);
	free(x);
}

//头文件
#ifndef _HEAD_H_

#define _HEAD_H_
typedef struct node* link;
struct node
{
	int item;
	link next;
};
 
  extern link reverse(link x);
  extern void print(link x);
  extern link initial(int N);
  extern link put_ahead(link x,int n);		
  extern void insert(link m,link n,int number);
  extern link put_last(link x,int n);
  extern link sort(link x);
  extern link another_sort(link x);
  extern void freenode(link x);
  extern link initial_circle(int N);
#endif
#ifndef _HEAD_H_

//函数 其中一些并没有用到
#include"head.h"
#include<stdio.h>
#include<stdlib.h>
link reverse(link x)
{
	link y;
	link t;
	link r;
	r=NULL;
	y=x;
	while(y!=NULL)
	{
		t=y->next;
		y->next=r;		
		r=y;
		y=t;
	}
	return r;
}
 void print(link x)
{
	while(x!=NULL)
	{
		printf("%d\t",x->item);
		x=x->next;
	}
	printf("--------------------------------------------\n");
}
 link initial(int N)
{
	int i;
	link x;
	link t;
	x=(t=malloc(sizeof *t));
	for(i=1;i<N;i++)
	{
		t->item=rand()%N;
		t->next=malloc(sizeof *t);
		t=t->next;
	}
	t->item=rand()%N;
	t->next=NULL;
	return x;
}
 link put_ahead(link x,int n)
{
	link t;
	link temp;
	t=malloc(sizeof *t);
	t->item=n;
	temp=x;
	x=t;
	t->next=temp;
	return x;
}
void insert(link m,link n,int number)
{
	link temp=malloc(sizeof *temp);
	temp->item=number;
	temp->next=n;
	m->next=temp;
}
link put_last(link x,int n)
{
	link t=x;
	link temp=malloc(sizeof *temp);
	temp->item=n;

	while(t->next!=NULL)
	{
		t=t->next;
	}
	temp->next=NULL;
	t->next=temp;
	return x;
}

	
 link sort(link x)
{
	link y;
	link m;
	int flag;
	y=malloc(sizeof *y);
	y->item=x->item;
	y->next=NULL;
	x=x->next;
	while(x!=NULL)
	{
		m=y;
		flag=1;
			if(x->item<= y->item)
			{
				y=put_ahead(y,x->item);
			}
			else
			{	while(m->next!=NULL)
				{
					if( !((x->item<m->item) || (x->item>m->next->item)))
					{
						insert(m,m->next,x->item);
						flag=0;
						break;
					}
					else
					{
						m=m->next;
					}
				 }
			     if(flag)
				 {
					 y=put_last(y,x->item);
				 }
	         }
			
		x=x->next;
	 }
	return y;
}

 link another_sort(link x)
 {
	 link y=malloc(sizeof *y);
	 link y_temp;
	 link x_temp;
	 link yy;
	 int flag;
	 y->next=x; 
	 x=x->next;
	 y->next->next=NULL;
	 while(x!=NULL)
	 {
		 yy=y;
		 y_temp=y->next;
		 while(y_temp!=NULL)
		 {
			 if(x->item<=y_temp->item)
			 {
				 yy->next=x;
				 x_temp=x;
				 x=x->next;
				 x_temp->next=y_temp;
				 flag=1;
				 yy=yy->next;
				 y_temp=y_temp->next;
				 break;
			 }
			 flag=0;
			 yy=yy->next;
			 y_temp=y_temp->next;
		 }
		 if(!flag)
		 {
			 yy->next=x;
			 x_temp=x;
			 x=x->next;
			 x_temp->next=NULL;
		 }
	 }
		 return y->next;
 }

void freenode(link x)
{
	while(x!=NULL)
	{
		free(x);
		x=x->next;
	}
	printf("x has been freed!\n");
}

link initial_circle(int N)
{
	int i;
	link x=malloc(sizeof *x);
	link y=x;
	for(i=1;i<N;i++)
	{
		y->item=i;
		y->next=malloc(sizeof *y);
		y=y->next;
	}
	y->item=N;
	y->next=x;
	return x;
}
	







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值