/**
*
* @author Sakura
* @feature 约瑟夫问题
*
*/
public class Test {
public static void main (String args[]) {
CycLink cycLink = new CycLink();
cycLink.setLen(5);
cycLink.createLink();
cycLink.setK(2);
cycLink.setM(2);
cycLink.play();
//cycLink.show();
}
}
class Child{
int no;
//下一个指向
Child nextChile = null;
//上一个指向
Child proveChile = null;
//构造方法
public Child(int no)
{
this.no = no;
}
}
class CycLink{
//链表的头标
Child firstChild = null;
//临时
Child temp = null;
//总数
private int len;
//设置人数的总数
public void setLen(int len) {
this.len = len;
}
//从哪开始
private int k;
public void setK(int k)
{
this.k = k;
}
//每次数几个
private int m;
public void setM(int m)
{
this.m = m;
}
//开始玩游戏
public void play() {
temp = this.firstChild;
//寻找开始的位置
for (int i=1; i<k;i++)
{
temp = temp.nextChile;
}
while(this.len !=1) {
//开始数数
for (int i=1;i<m;i++)
{
temp = temp.nextChile;
}
//删除
temp.proveChile.nextChile = temp.nextChile;
temp.nextChile.proveChile = temp.proveChile;
System.out.println(temp.no);
temp = temp.nextChile;
this.len--;
}
}
//创建循环链表
public void createLink() {
for (int i=1; i<=len; i++)
{
if (i==1)
{
Child ch = new Child(i);
this.firstChild = ch;
this.temp = ch;
}
else if (i==len)
{
Child ch = new Child(i);
temp.nextChile = ch;
temp.nextChile.proveChile = temp;
this.temp = ch;
this.firstChild.proveChile = temp;
temp.nextChile = this.firstChild;
}
else
{
Child ch = new Child(i);
temp.nextChile = ch;
temp.nextChile.proveChile = temp;
this.temp = ch;
}
}
}
//展示
public void show()
{
Child temp = this.firstChild;
do {
System.out.println(temp.proveChile.no);
temp = temp.proveChile;
}while(temp != this.firstChild);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
约瑟夫问题_基于java+双链表实现的约瑟夫问题题解.zip (7个子文件)
约瑟夫问题_基于java+双链表实现的约瑟夫问题题解
.classpath 386B
.settings
org.eclipse.jdt.core.prefs 630B
src
Test.java 2KB
bin
Test.class 560B
Child.class 375B
CycLink.class 2KB
.project 363B
共 7 条
- 1
资源评论
JZSJ
- 粉丝: 2w+
- 资源: 1325
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功