队列:约瑟夫问题
作者:
羽笙
,
2024-05-20 06:34:53
,
所有人可见
,
阅读 11
同样是将顾客加入队列,进行报数模拟
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 1010,M = 1e6+10;
int n,m;
int q[M],front = 1,rear;
signed main()
{
cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
cin>>n>>m;
int cnt = 0;
for(int i = 1;i<=n;i++)q[++rear] = i;
while(rear >= front)
{
cnt++;
if(cnt == m)
{
cout<<q[front]<<" ";
cnt = 0;
}
else
{
q[++rear] = q[front];
}
front++;
}
return 0;
}