约瑟夫问题
2015-10-16 21:52
239 查看
#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(struct node)); p=head; node *s; int i=1; if(0!=n) { while(i<=n) { s=(node*)malloc(sizeof(node));//»¹±ØÐëÊÇ*node s->data=i; 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=m%n; while(p!=p->next) { for(i=1;i<m-1;i++) { p=p->next; } printf("%d->",p->next->data); temp=p->next; p->next=temp->next; free(temp); p=p->next; } printf("%d\n",p->data); getchar(); }
相关文章推荐
- Dubbo入门实例
- 设置外网映射
- Linq 单表城市级联
- 网络时代如何尽量保证自己不被骗?(二)一个诈骗短信的例子
- 按字节提取整形数值(按位与运算符“&”、右移位运算符“>>”)
- 栈的链式实现
- Java核心技术第11章(1)
- ByteBuffer.allocate()与ByteBuffer.allocateDirect()方法的区别。
- Weblogic11g下调WebService出现的一系列问题
- Linux学习 -- Shell基础 -- 概述
- 算法录 之 基础算法
- Weblogic11g下调WebService出现的一系列问题
- 华为招聘测试分选机6:才艺展示率
- Python 3.x标准模块库目录
- 阿里巴巴宣布将以45亿美元收购优酷土豆
- 阿里巴巴宣布将以45亿美元收购优酷土豆
- hibernate编程中的一个错误及成因
- Fragment之间的通信
- java演示适配器(adapter)模式
- centos 6,7的网络配置