约瑟夫环

9 篇文章 0 订阅

约瑟夫环 好像实现不了 没时间调了 各位帮帮看看

#include <iostream>
#include <cstring>

using namespace std;

typedef struct listpoint
{
	int data;
	listpoint *next;
	listpoint *last;

}listpoint;

listpoint *creat_noraml_list(int n)
{
	 listpoint *head,*normal,*end;
	 head = (listpoint*)malloc(sizeof(listpoint));
	 end = head;
	 for(int i = 1;i<=n;i++){
		 normal = (listpoint*)malloc(sizeof(listpoint));
		 normal->data = i;
		 end->next = normal;
		 normal->last = end;
		 end = normal;                    //最后一个节点变成新节点
		 cout<<"检测输入数据地址:"	<< normal<<endl;
	 }
	 end->next = head;
	 head->last = end;
	 cout<<"头节点地址"<<head<<endl;
	 
	 return head;
}

listpoint* listn( listpoint* l1,int n)
{
	listpoint*l2=l1;
	for (int i = 1; i <= n; i++)
	{
		if (l2->next!=NULL)
		{
			l2 = l2->next;
			
		}
	}
	return l2;

}

void delete_Point(listpoint*list,int n)
{
	cout<<"头节点地址"<<list<<endl;
	listpoint *p = list;
	for (int i = 0; i < n; i++){
		if(p->next!=NULL) p = p->next;
	}
	p->next->last = p->last;
	p->last->next = p->next;
	free(p);
}


int fun(listpoint*list,int m,int n)
{
	int data;
	listpoint *p;
	int pos = 1;  //记录当前位置
	int last_pos = n;
	while(last_pos!=1){
	   pos = pos + m -1;
	   while (pos>last_pos) pos -= last_pos;
		   delete_Point(list,pos);
		   last_pos--;
	 }
	for (int i = 0; i < n; i++)
	{
		if (list->next!=NULL)
		{
			list = list->next;
		}
	}
	cout<<"最终节点地址"<<list<<endl;
	system("pause");
	data = list->data;
	return data;

}


int main()
{
	int m = 7;
	int n = 30;
	listpoint* list;
	list = creat_noraml_list(n);
	int data = fun(list,m,n);
	cout<<"最终赢家  "<< data;
	system("pause");


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值