约瑟夫问题

传送门:https://www.luogu.org/problemnew/show/P1996#sub

模拟题意,具体详见代码

#include<cstdio>
using namespace std;
int n,m,tot,last = 1,cnt;
int a[101];
int main()
{
    scanf("%d%d",&n,&m);
    tot = n;
    while(tot)//当还有人没出圈 
    {
        cnt = 0;
        for(int i = last;cnt != m;i++)
        {
            if(i > n) i %= n;//保证在n个人中循环 
            if(a[i] == -1) continue;//如果这个人已经出圈,跳过 
            ++cnt;//否则计数新一轮数到了第几个人 
            if(cnt == m) 
            {
                a[i] = -1;// 数到了m的人出圈
                tot--;//总人数-1 
                last = i + 1;//从出圈的人下一个开始重新数 
                printf("%d ",i);
                break;
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/peppa/p/9878263.html