6 solutions

  • 0
    @ 2025-3-29 23:43:32

    queue or 数组模拟

    #include <bits/stdc++.h>
    #define endl '\n'
    using namespace std;
    typedef pair<int, int> PII;
    using ll = long long;
    using ULL = unsigned long long;
    const int N = 1e7 + 5;
    
    int n, k, a[N];
    //queue<int> q;
    int q[N], hh = 0, tt = -1;
    inline void solve() {
    /*
        cin >> n >> k;
        for (int i = 1; i <= n; i++) q.push(i);
        ll cnt = 0, id = 0;
        while (!q.empty()) {
            int hh = q.front();
            q.pop();
            cnt++;
            if (cnt == k) {
                id++;
                a[hh] = id;
                cnt = 0;
            }else q.push(hh);
        } 
        for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i==n];
    */
        cin >> n >> k;
        for (int i = 1; i <= n; i++) q[++tt] = i;
        int cnt = 0, idx = 0;
        while (hh <= tt) {
            int aa = q[hh];
            ++hh;
            cnt++;
            if (cnt == k) {
                idx++;
                a[aa] = idx;
                cnt = 0;
            }else q[++tt] = aa;
        }
       for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i==n];
    }
    
    int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        int _ = 1;
        //int _; cin >> _;
        while (_--) solve();
        return 0;
    }
    
    
    • 0
      @ 2025-3-24 15:45:03
      #include<iostream>
      #include<cmath>
      #include<algorithm>
      #include<queue>
      using namespace std;
      using ll = long long;
      const int N = 1e3+10;
      ll a[N];
      int main()
      {
          int n,k;cin>>n>>k;
          int idx = 0;
          int cnt = 0;
          queue<int> q;
      
          for(int i = 1;i <= n;i++)
          {
              q.push(i);
          }
      
          while(!q.empty())
          {
              int hh = q.front();
              q.pop();
              cnt ++;
              if(cnt == k)
              {
                  idx++;
                  a[hh] = idx;
                  cnt = 0;
              }else q.push(hh);
          }
          for(int i = 1;i <= n;i++)cout<<a[i]<<" ";
         
          return 0; 
      }
      
      
      • 0
        @ 2024-10-26 10:24:46

        c

        #include<stdio.h> int main() { int m,n; int temp[1000]; int t=0;//记第几个死 int f[1000]; scanf("%d %d",&m,&n); for(int i=0;i<m;i++) { f[i]=i+1; }//标号每个人的站位 int i=0; int k=0;//报数 int c=m;//表示还剩的人数 while(c>0)//死光了就不循环了 { if(f[i]!=0)//死人不用报数 k++;//开始报数 if(k==n) { t++; temp[i]=t; c--; f[i]=0;//死人腾出站位 k=0;//重新报数 } i=(i+1)%m;//围成了圈,当i=m-1时,i=0,又重新开始数
        } for(int j=0;j<m;j++) printf("%d ",temp[j]); return 0; }

        • 0
          @ 2022-8-6 22:15:00

          这道题是一道经典的循环链表

          #include <bits/stdc++.h>
          
          //循环链表
          
          using namespace std;
          
          const int maxn = 1e3 + 5;
          int id[maxn];
          
          typedef struct Node {
          int value;
          Node *next;
          }NodeList;
          
          int main(void) {
          int n, m;
          cin >> n >> m;
          NodeList *head, *last = NULL;
          NodeList *node;
          
          for (int i = 1; i <= n; i++) {
              node = (NodeList*)malloc(sizeof(NodeList));
              node->value = i;
              if (i == 1) {
                  head = node;
                  last = node;
              }else {
                  last->next = node;
                  last = node;
              }
          }
          node->next = head;
          last = node;
          
          int i = 1;
          int cnt = 1;
          while (head->next != head) {
              if (i == m) {
                  NodeList *p = head;
                  last->next = head->next;
                  id[p->value] = cnt;
                  cnt ++;
                  free(p);
                  i = 1;
                  head = last->next;
              }else {
                  last = head;
                  head = head->next;
                  i ++;
              }
          }
          
          for (int i = 1; i <= n; i++) {
              if (!id[i]) id[i] = n;
              cout << id[i] << ' ';
          }
          
          return 0;
          

          }

          • 0
            @ 2022-4-1 8:05:52

            约瑟夫环 经典题了,用 队列 随别 虐杀。数组的话 也可以。

            #include <iostream>
            #include <queue>
            
            using namespace std;
            
            int arr[1005]; // 到底是 第几次 自杀的 都存在 这里
            
            int main(void) {
            	int n, k;// n 个 人,报数到 k 就会自杀
            	cin >> n >> k;
            
            	queue<int> que;
            
            	for (int i = 1; i <= n; ++i) {
            		que.push(i); // 先把这些人 都添加到 队列里
            	}
            
            	int ans = 0;// 记录是第几个 出去的
            	int jilu = 0; // 记录 数到 几了
            	while (que.size()) {
            		int id = que.front();// 编号
            		que.pop();
            		jilu++;
            		if (jilu != k) {
            			que.push(id);
            		}
            		else {
            			ans++;
            			arr[id] = ans;
            			jilu = 0;
            		}
            	}
            
            	for (int i = 1; i <= n; ++i) {
            		cout << arr[i] << " ";
            	}
            
            	return 0;
            }
            
            • 0
              @ 2022-3-9 21:30:00
              #include <stdio.h>
              #include <algorithm>
              #include <math.h>
              #include <vector>
              #include <iostream>
              #include <string.h>
              #include <queue>
              using namespace std;
              int n,k;
              queue<int> q;
              int a[1005];
              int main() {
                  scanf("%d%d",&n,&k);
                  for(int i=1; i<=n; i++) {
                      q.push(i);
                  }
                  int sum=1,i=0,j=0;
                  while(sum<n) {
                      if(j!=k-1) {
                          int tmp=q.front();
                          q.pop();
                          q.push(tmp);
                          j++;
                      } else {
                          a[q.front()]=sum;
                          sum++;
                          q.pop();
                          j=0;
                      }
                  }
                  a[q.front()]=sum;
                  for(int i=1; i<=n; i++)
                      printf("%d ",a[i]);
                  return 0;
              }
              
              
              • 1

              Information

              ID
              79
              Time
              1000ms
              Memory
              256MiB
              Difficulty
              6
              Tags
              # Submissions
              303
              Accepted
              103
              Uploaded By