intmain() { int n, m; cin >> n >> m; //新建链表节点 for (int i = 1; i <= n; ++i) { arr[++tot] = Node(tot, arr[tot].nxt_, i); arr[tot - 1].nxt_ = tot; } //下标0是伪节点,应从下标1开始遍历 int now = 1; while (dummyhead.nxt_) { for (int i = 1; i < m; ++i) { now = arr[now].nxt_; if (!now) now = dummyhead.nxt_; } int nxt = arr[now].nxt_; if (!nxt) nxt = dummyhead.nxt_; cout << arr[now].key_ << " "; del(now); now = nxt; } return0; }
intmain() { int n, m; cin >> n >> m; queue<int> q; for (int i = 1; i <= n; ++i) q.emplace(i); while (!q.empty()) { for (int cnt = 1; cnt < m; ++cnt) { q.emplace(q.front()); q.pop(); } cout << q.front() << " "; q.pop(); } return0; }