#include<bits/stdc++.h>
using namespace std;
typedef struct node {
int s;
node *next;
};
node* creat(int m) {
node* head, * p, * q;
p = (node*)malloc(sizeof(node));
head = p;
for (int i = 0; i < m; i++) {
q = (node*)malloc(sizeof(node));
q->s = i + 1;
p->next = q;
q->next = NULL;
p = q;
}
p->next = head->next;
return head;
}//创建循环链表
int main() {
int n;
cin >> n;
node* h = creat(n);
h = h->next;
int m;
cin >> m;
int js=1;
for (int i = 0; i < n; i++) {
while (++js) {
if (js == m) {
cout << h->next->s<<" ";
h->next = h->next->next;//删除节点
js = 1;//重新报数
h = h->next;
break;
}
h = h->next;
}
}
}
利用循环链表求解约瑟夫环问题
最新推荐文章于 2024-08-12 18:10:44 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)