【codevs 1282】 约瑟夫问题 【题解】

本文介绍了约瑟夫问题的模拟方法,通过使用STL队列构建二叉树来表示存活的人,并在删除节点后快速找到下一个要删除的节点。通过不断删除节点,模拟问题过程。文中提到,虽然普通二叉树即可,但平衡二叉树或块链也是可行的选择,且可能更易实现。

模拟:

STL队列:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#define inf 0x3f3f3f3f
#define LL unsigned long long
#include<queue>
using namespace std;
int n,m;
queue<int> q;
/******Program Begin*********/
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)q.push(i);
	int cnt=0;
	while(!q.empty())
	{
		int t = q.front();
		q.pop();
		cnt++;
		if(cnt%m==0)
		{
			cnt=0;
			printf("%d ",t);
			continue;
		}
		else q.push(t);
	}
	return 0;
}
/*******Program End**********/


#include"stdio.h"

int main()

{

int m,n,i,j;

int s=1;

int a[30005];

scanf("%d%d",&n,&m);

for(i=0;i<=n;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真·skysys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值