约瑟夫问题-链表实现
来源:互联网 发布:java通过url上传文件 编辑:程序博客网 时间:2024/06/06 15:53
约瑟夫问题_百度百科 http://baike.baidu.com/link?url=EuXtXV4hPi1AtRdzY0vMPG1gQg1kt3uCS5g4Bfy_RzWXAnDwwjJkhxWdRfk2LPnkqubRBTb52CXEaUa5jbIRaK
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define RECURSION//Monkey Node definitiontypedef struct node{ int number;struct node *next;}node;//Josephus Problem#ifdef RECURSIONvoid king(struct node *list,int m){int i;struct node *pout=list;if(pout->next != pout) {//one more monkeysfor(i=1; i<m; i++)pout=pout->next;for(list=pout; list->next != pout;) list=list->next;printf("%5d",pout->number);//show the monkey to be outlist->next = pout->next;//get out of listfree(pout);//free the nodeking(list->next,m);//RECURSION}else//the last one monkeyprintf("\nThe king is the monkey %d\n",list->number);}#elsevoid king(struct node *list,int m){int i;struct node *pout=NULL;for(pout=list;pout->next!= pout;pout=list->next){for(i=1; i<m; i++)pout=pout->next;for(list=pout; list->next != pout;) list=list->next;printf("%5d",pout->number);//show the monkey to be outlist->next = pout->next;//get out of listfree(pout);//free the node}//the last one monkeyprintf("\nThe king is the monkey %d\n",list->number);}#endif//generate the cycle list of n monkeys (n>=1)struct node* CreateCycle(int n){int i;struct node *head,*tail,*newnode;tail = head = (struct node*)malloc(sizeof(struct node));head->number = 1;head->next = head;for(i=2;i<=n;i++){newnode = (struct node*)malloc(sizeof(struct node));newnode->number = i;newnode->next = head;tail->next = newnode;tail=newnode;}return head;}int main(void){int total,out;struct node *list=NULL;// n monkeys in totaldo{printf("How many monkeys in total:");scanf("%d",&total);}while(total<1);//which monkey gets out each timedo{printf("Which monkey to be out:");scanf("%d",&out);}while(out<1);list = CreateCycle(total);printf("monkeys out in order:\n");king(list,out);return 0;}
0 0
- 约瑟夫问题-链表实现
- 约瑟夫环问题 链表实现
- 链表实现约瑟夫环问题
- 利用双向链表实现约瑟夫问题
- 单向循环链表实现约瑟夫问题
- 用循环链表实现约瑟夫问题
- 约瑟夫问题简单实现-循环链表
- 约瑟夫问题的链表实现
- 用单循环链表实现约瑟夫问题。
- C++循环链表实现约瑟夫问题
- 约瑟夫问题--循环链表实现
- 循环链表 约瑟夫环问题实现
- 约瑟夫环问题循环链表实现
- 约瑟夫问题——链表实现
- 约瑟夫问题循环链表实现
- 约瑟夫环问题的链表实现
- 环形链表+约瑟夫问题实现
- 约瑟夫问题(循环链表实现)
- HEAP[xxx.exe]:Invalid Address specified to RtlValidateHeap 错误的解决方法总结
- android_自动检测更新
- close form 封闭解
- android瀑布流效果(仿蘑菇街)
- Mac系统 与 Xcode快捷键整理
- 约瑟夫问题-链表实现
- QR二维码Java开发笔记
- linux下启动、关闭tomcat,linux设置tomcat开机自启动
- 错误异常表
- 15年回顾--个人规划路线
- C语言小写字母转换为大写字母
- selenium中为什么有些IE浏览器中输入英文和数字特别慢
- 23种设计模式有感
- ssh无密码登录注意事项