3 solutions

  • 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
        5
        Tags
        # Submissions
        220
        Accepted
        84
        Uploaded By