C语言笔记34 •单链表经典算法OJ题-6.环形链表的约瑟夫问题•

环形链表的约瑟夫问题

1.问题

编号为 1 到 n 的 n 个人围成一圈。从编号为 1 的人开始报数,报到 m 的人离开。

下一个人继续从 1 开始报数。

n-1 轮结束以后,只剩下一个人,问最后留下的这个人编号是多少?

数据范围: 1≤𝑛,𝑚≤100001≤n,m≤10000

进阶:空间复杂度 𝑂(1)O(1),时间复杂度 𝑂(𝑛)O(n)

2.代码实现

//6.环形链表的约瑟夫问题
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>




typedef int SLTDataType;

typedef struct SListnode
{
	SLTDataType val;
	struct SListnode* next;
}ListNode;

ListNode* createNode(SLTDataType val)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(1);
	}
	newnode->val = val;
	newnode->next = NULL;
	return newnode;
}

//创建带环链表
ListNode* createCircleList(int n)
{
	ListNode* phead= createNode(1);
	ListNode* ptail = phead;
	for (int i = 2; i <= n; i++)
	{
		ptail->next = createNode(i);
		ptail = ptail->next;
	}
	ptail->next = phead;//首尾相连,链表成环
	return ptail;
}

int person_num(int n,int m)
{
	ListNode* ptail = createCircleList(n);
	ListNode* pcur = ptail->next;
	int count = 1;
	while (ptail->next != ptail)
	{
		if (count == m)//销毁节点
		{
			ptail->next = pcur->next;
			free(pcur);
			pcur = ptail->next;
			count = 1;
		}
		else
		{
			ptail = ptail->next;//等价于ptail = pcur;
			pcur  = pcur->next;
			count++;
		}
	}
	return ptail->val;

}

int main()
{
	int person = person_num(5, 2);
	printf("%d", person);

	return 0;
}

相关推荐

  1. C++ 环形(解决问题)

    2024-07-14 06:24:02       28 阅读
  2. 环形问题

    2024-07-14 06:24:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 06:24:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 06:24:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 06:24:02       58 阅读
  4. Python语言-面向对象

    2024-07-14 06:24:02       69 阅读

热门阅读

  1. VECTOR,ARRAYLIST, LINKEDLIST的区别是什么?

    2024-07-14 06:24:02       27 阅读
  2. 持续集成的自动化之旅:Gradle在CI中的配置秘籍

    2024-07-14 06:24:02       23 阅读
  3. C++:虚函数相关

    2024-07-14 06:24:02       27 阅读
  4. helm系列之-构建自己的Helm Chart

    2024-07-14 06:24:02       22 阅读
  5. (算法)硬币问题

    2024-07-14 06:24:02       24 阅读
  6. 【代码复现】STAEformer

    2024-07-14 06:24:02       21 阅读