c++ 约瑟夫问题 (game)

  • 题目:
  • 总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 512000kB
  • 描述
  • 设编号分别为:1,2,...,n的n个人围坐一圈。从序号为1的人开始报数,数到m的那个人出列,他的下一位又从1开始报数,数到m的那个人再出列,依次类推,直到所有人出列为止。
  • 输入
  • 共1行:人数n和报数m,用空格隔开。
  • 输出
  • 共1行,出圈序列。
  • 样例输入
  • 6 5
  • 样例输出
  • 5 4 6 2 3 1
  • 提示
  • 1≤n≤30000,1≤m≤n。

我的代码:

#include<bits/stdc++.h>
using namespace std;
int n,m,cnt,e,a[30000];
int main()
 {
     cin>>n>>m;
    for(int i=1;i<=n;i++) a[i]=i;
    for(int i=1;;i++)
    {
         if(a[i]!=-1)cnt++;
         if(cnt==m&&a[i]!=-1)
         {
           cout<<a[i]<<" ";
           a[i]=-1;
           e++;
           cnt=0;
         }
         if(i==n) i=0;        if(e==n) break;
     }
     return 0;
 }

为什么 Time Limit Exceeded

https://blog.csdn.net/weixin_41747893/article/details/99337212

https://blog.csdn.net/kuller_Yan/article/details/104204337