#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * next;
};
int main(){
int N;
int M;
printf("please enter the nubmer of N:\n");
scanf("%d",&N);
printf("please enter the nubmer of M:\n");
scanf("%d",&M);
struct node * point;
point=(struct node *)malloc(sizeof(struct node));
point->num=1;
point->next=point;
struct node * tail;
struct node * head;
head=point;
tail=point;
for(int i=1;i<N;i++){
struct node * point;
point = (struct node *)malloc(sizeof(struct node));
point->next=head;
tail->next=point;
tail=tail->next;
point->num=i+1;
}
while(head->next != head){
for(int i=0;i<M-2;i++){
head=head->next;
}
head->next=head->next->next;
head=head->next;
}
printf("the last one is %d\n",head->num);
return 0;
}josephus problem
最新推荐文章于 2025-02-02 15:16:21 发布
本文介绍了一个使用链表实现的约瑟夫环问题解决方案。通过C语言编程,该程序首先构建一个包含N个节点的循环链表,然后按照M步长移除节点直至最后一个节点,最后输出该节点的编号。
582

被折叠的 条评论
为什么被折叠?



