约瑟夫问题-队列

2018-04-02 17:41:52


#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
queue<int>q;
int vis[10001];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    q.push(i);
    int now=1;
    while(!q.empty())
    {
        for(int i=1;i<=m;i++)
        {
            if(now==m)
        {
            now=1;
            printf("%d ",q.front());
            q.pop();
        }
            else 
        {
            now++;
            q.push(q.front());
            q.pop();
        }
        }   
    }
    return 0;
}