约瑟夫问题循环链表题解来了!!!
#include<bits/stdc++.h>
using namespace std;
int m, n;
struct Node{
int data;
Node *next;
} *head, *p, *tail, *temp;
int main(){
cin>>m>>n;
head = new Node;
head -> next = NULL;
tail = head;
for(int i = 1; i <= m; i++){
p = new Node;
p -> data = i;
p -> next = NULL;
tail -> next = p;
tail = p;
}
p = head -> next;
tail -> next = head -> next;
for(int i = 1; i <= m; i++){
for(int j = 1; j < n - 1; j++){
p = p -> next;
}
cout<<p -> next -> data<<" ";
temp = p -> next;
p -> next = temp -> next;
p = p -> next;
free(temp);
}
return 0;
}
标签:Node,head,int,next,链表,tail,循环,约瑟夫,data
From: https://blog.csdn.net/2401_84500159/article/details/144935856