用循环链表来实现:
#include
#include
struct node
{
int num;
struct node* next;
};
int main(void)
{
int i, n, m;
scanf("%d%d", &n, &m);
node* p = (node*)malloc(sizeof(node));
node* x = p;
p->num = 1;
p->next = p;
for(i = 2; i <= n; i++)
{
x->next = (node*)malloc(sizeof(node));
x = x->next;
x->num = i;
x->next = p;
}
while(x->next != x)
{
for(i = 1; i < m; i++)
x = x->next;
printf("%d ", x->next->num);//依次输出排除的数
x->next = x->next->next;
}
system("pause");
}