UVA 133 The Dole Queue 约瑟夫问题的变种,手写链表,模拟
来源:互联网 发布:自己设计字体的软件 编辑:程序博客网 时间:2024/05/06 09:32
连约瑟夫问题的公式都不会,更何况变种了,只好手动模拟
好吧,我确实想到了删除一个数就把它置为0,然后要走的时候再判断...
可是我觉得一旦n变大了,这种方法需要的时间也随之上升,因此手写一个链表来走好了
le[i]代表第i个元素左边的元素的下标,ri[i]代表第i个元素右边元素的下标 ,sz表示链表里还有多少元素。
不过因为我的实现的缘故,依然要把删除的元素置为0,不过我觉得还是比原来的方法要快
删除的时候只要修改左边元素的ri和右边元素的le就好了,以下是代码
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int n,k,m,arr[25],be,ed,le[25],ri[25],sz;int main(){ while(scanf("%d%d%d",&n,&k,&m)==3&&n){ sz=n; for(int i=0;i<n;++i) arr[i]=n-i,le[i]=(i-1+n)%n,ri[i]=(i+1)%n; be=n-1,ed=0; while(sz){ for(int i=1;i<k;++i) be=le[be]; for(int i=1;i<m;++i) ed=ri[ed]; if(sz==1&&be==ed){ printf(" %2d\n",arr[be]); break; } else if(sz==2&&be!=ed){ printf(" %2d %2d\n",arr[be],arr[ed]); break; } else if(be==ed){ printf(" %2d,",arr[be]); --sz; arr[be]=0; ri[le[be]]=ri[be]; le[ri[be]]=le[be]; while(!arr[be]) be=le[be]; while(!arr[ed]) ed=ri[ed]; } else{ printf(" %2d %2d,",arr[be],arr[ed]); arr[be]=arr[ed]=0; sz-=2; ri[le[be]]=ri[be]; le[ri[be]]=le[be]; ri[le[ed]]=ri[ed]; le[ri[ed]]=le[ed]; while(!arr[be]) be=le[be]; while(!arr[ed]) ed=ri[ed]; } } } return 0;}
0 0
- UVA 133 The Dole Queue 约瑟夫问题的变种,手写链表,模拟
- uva 133 The Dole Queue 双向约瑟夫环 模拟实现
- UVa 133 The Dole Queue (模拟循环链表)
- UVA - 133 The Dole Queue(模拟链表)
- uva 133 The Dole Queue(约瑟夫变形)
- UVA 133 The Dole Queue 【约瑟夫环】
- UVa-133 The Dole Queue(约瑟夫环)
- Uva 133 - The Dole Queue//模拟,链表
- uva 133 The Dole Queue(模拟)
- UVa 133 - The Dole Queue【模拟】
- uva133 The Dole Queue ( 约瑟夫环的模拟)
- Uva 133 The Dole Queue 双向约瑟夫环
- The Dole Queue 约瑟夫环 模拟
- UVa 133 The Dole Queue
- Uva 133 The Dole Queue
- uva 133 - The Dole Queue
- uva-133 - The Dole Queue
- UVa 133 - The Dole Queue
- 网省变电精益化配置
- android平台的三个编译命令----make,mm,mmm
- 斯坦福机器学习公开课8-1 2 神经网络初步了解
- Java多线程编程Runable,Callable,Feature区别和用法
- Servlet实现用户的登录注册+sqlite数据库验证
- UVA 133 The Dole Queue 约瑟夫问题的变种,手写链表,模拟
- 拍照上传
- 史上最全最强SpringMVC详细示例实战教程
- STM32串口通信
- Mongodb 3.2 Java 显示指定字段 条件查询 官方
- caffe 网络模型可视化python例程
- Atom VS Sublime Text 文本编辑器-捣鼓记
- as错误could not install smartsocket listener: cannot bind to 通常每个套接字地址(协议/网络地址/端口)只允许使用一次
- mybaits使用