您的位置:首页 > 其它

josephus problem

2014-01-27 09:49 197 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: