3 条题解

  • 0
    @ 2024-8-25 11:26:32

    数组方式:

    #include <bits/stdc++.h>
    #define N 1001
    using namespace std;
    int q[N];
    int main()
    {
        int n,m;
        cin>>n>>m;
    
        for(int i=1;i<=n;i++)
            q[i]=i;
    
        int head=1,tail=n+1;
        int cnt=0;
        while(head<tail)
        {
            cnt++;
            int p=q[head];
            if(cnt==m)
            {
                cnt=0;
                cout<<p<<" ";
            }
            else
            {
                q[tail]=p;
                tail++;
            }
            head++;
        }
        cout<<endl;
        return 0;
    }
    
    • 0
      @ 2024-8-11 8:41:50

      方法3:

      #include <bits/stdc++.h>
      using namespace std;
      int main() {
          int n, m;
          cin >> n >> m;
          queue<int> a;
          for (int i = 0; i < n; i++) {
              a.push(i);
          }
          int c = 1;
          while (!a.empty()) {
              int x = a.front();
              a.pop();
              if (c == m) {
                  cout << x + 1 << " ";
                  c = 1;
              } else {
                  c++;
                  a.push(x);
              }
          }
          return 0;
      }
      
      • 0
        @ 2024-8-11 8:41:05

        递归方法:

        #include <bits/stdc++.h>
        int kun(int m, int n, int i) {
            if (i == 1) {
                return (n - 1) % m;
            } else {
                return (kun(m - 1, n, i - 1) + n) % m;
            }
        }
        
        int main() {
            int m, n;
            scanf("%d%d", &m, &n);
            for (int i = 1; i <= m; i++) {
                printf("%d ", kun(m, n, i) + 1);
            }
            return 0;
        }
        
        • 1

        信息

        ID
        812
        时间
        1000ms
        内存
        256MiB
        难度
        7
        标签
        递交数
        14
        已通过
        11
        上传者