#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node *next;
};
int main(int argc, char *argv[])
{
int i, N = atoi(argv[1]), M = atoi(argv[2]);
struct node *t;
struct node *x;
t = (struct node *)malloc(sizeof(struct node *));
x = t;
t->item = 1;
t->next = t;
for (i = 2; i <= N; i++)
{
x = (x->next = (struct node *)malloc(sizeof(struct node *))); //生成一个环后x指向最后一个节点,x->next才指向第一个节点
x->item = i;
x->next = t;
}
while (x != x->next)
{
for (i = 1; i < M; i++)
x = x->next;
printf("%d->", x->next->item); //此处应该输出x->next->item
x->next = x->next->next;
N--;
}
printf("%d/n", x->item);
system("pause");
return 0;
}
约瑟夫环:出队顺序
最新推荐文章于 2022-09-04 16:35:55 发布