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; }
相关文章推荐
- Josephus Problem
- 约瑟夫环问题(Josephus_problem)
- Josephus Problem
- 湘潭市赛 Josephus Problem 线段树
- Josephus Problem
- lightoj 1179 - Josephus Problem 约瑟夫问题
- 约瑟夫问题Josephus problem
- 约瑟夫问题(Josephus problem)
- 约瑟夫问题(Josephus problem)1:出列的序列
- Josephus Problem的详细算法及其Python, Java语言的实现
- Josephus Problem
- 湘潭市赛 Josephus Problem 线段树
- 约瑟夫问题(Josephus problem)2:某人何时出列
- 约瑟夫问题(Josephus Problem)的两种快速递归算法
- lightoj 1179 - Josephus Problem 约瑟夫环
- 湘潭市赛 Josephus Problem 线段树
- 约瑟夫问题(Josephus Problem)
- 约瑟夫问题(Josephus Problem)3:谁最后一个出列
- 约瑟夫环问题(josephus problem)详解
- Josephus problem