#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
最新推荐文章于 2017-06-18 14:36:04 发布