解法1:用一个头尾相接的链表模拟
#include<iostream>
using namespace std;
const int maxn = 100 + 10;
struct node
{
node(node* ptr, int i)
{
next = ptr;
index = i;
};
node * next;
int index;
};
int main()
{
int n, m;
node * head, *p = nullptr,*current;
while (cin >> n >> m)
{
head = p = new node(nullptr, n);
for (int i = n - 1 ; i >=1; i--)
{
p = new node(p,i);
//if (i == n)head = p;
}
head->next = p;
//p->next = head;
//p = head;
while (n--)
{
for (int i = 0; i < m - 1; i++)
{
//cout << p -> index << endl;
head = p;
p = p->next;
}
current = p;
head ->next = p->next;
cout << p->index ;
if (n != 0) cout << " ";
p = p->next;
delete current;
}
}
}
解法2:待补充