[数据结构]约瑟夫问题
来源:互联网 发布:access数据库 网页 编辑:程序博客网 时间:2024/06/13 09:02
//约瑟夫问题,n个人围圈报数,报m的出列,最后剩下的是几号?#include <stdio.h>#include <stdlib.h>//结构体表示链表的结点typedef struct node{ int data; struct node *next;}node;node *create(int n){ node *p = NULL, *head; head = (node*)malloc(sizeof (node ));//分配头结点 p = head;//指向当前结点的指针 node *s; int i = 1; if( 0 != n ) { while( i <= n ) { s = (node *)malloc(sizeof (node)); s->data = i++; // p->next = s; p = s; } s->next = head->next; } free(head); return s->next ;}int main(){ int n = 41; int m = 3; int i; node *p = create(n); node *temp; m %= n; printf("出队列的顺序是:\n"); while (p != p->next ) { for (i = 1; i < m-1; i++) { p = p->next ; } printf("%d->", p->next->data ); temp = p->next ;//删除第m个结点 p->next = temp->next ; free(temp); p = p->next ; } printf("\n\n最后留下来的人的号码是:%d\n", p->data ); return 0;}
0 0
- 数据结构之约瑟夫问题
- 数据结构-----约瑟夫环问题
- 数据结构:约瑟夫环问题
- [数据结构]约瑟夫环问题
- 数据结构—约瑟夫问题
- [数据结构]约瑟夫问题
- 数据结构 约瑟夫环问题
- 【数据结构基础】约瑟夫问题
- 数据结构--约瑟夫问题
- 数据结构--约瑟夫问题
- 数据结构算法问题 约瑟夫问题
- 《数据结构》“约瑟夫问题”解法征集
- 数据结构值约瑟夫环问题
- 数据结构与算法-约瑟夫问题
- 数据结构 约瑟夫环问题C++
- 数据结构与算法:约瑟夫问题
- 数据结构与算法---约瑟夫问题
- 数据结构 C语言 约瑟夫问题
- 掌握 tar 命令让你秒变大牛
- Cordova 打包 Android release app 过程详解
- Class.getSimpleName()的作用(转载)
- 机器学习中的线性和非线性判断
- Android系统源码编译环境搭建(清华镜像,想编译什么版本都可以,只是有JDK和make的版本要求)
- [数据结构]约瑟夫问题
- web前端-CSS 导航栏 -024
- Lua语法小贴士(三)魔法字符
- APK植入替换编码问题
- mysql查询实现新闻置顶功能
- 随笔
- Redhat环境下编译安装bazel
- hadoop 指标说明
- ViewTreeObserver简介