黑马程序员技术交流社区
标题:
丢手绢的算法问题之双链表
[打印本页]
作者:
麦子
时间:
2013-6-12 10:34
标题:
丢手绢的算法问题之双链表
package com.soke.demo6;
public class PlayHandkerchief {
/**
* 功能:解决丢手绢问题,采用不带头结点的双链表来解决
*/
public static void main(String[] args) {
DLinkList c1 = new DLinkList();
c1.setLength(10);
c1.setK(2);
c1.setM(2);
c1.createDLinkList();
c1.show();
c1.play();
}
}
//创建一个结点类
class Child{
int no;//结点数据域上的数
Child priorChild;//当前结点的前一个结点
Child nextChild;//当前结点的下一个结点
public Child(int no){
this.no = no;
}
}
//创建一个双链表类
class DLinkList{
Child firstChild = null;//定义一个头结点
Child temp = null;//定义一个跑龙套的结点
int length = 0 ; //定义双链表的长度
int k = 0;//从第K个人开始游戏的
int m = 0;//数到第m个数的人就退出游戏
public void setLength(int length) {
this.length = length;
}
public void setK(int k) {
this.k = k;
}
public void setM(int m) {
this.m = m;
}
//初始化双链表
public void createDLinkList(){
for(int i=1;i<=length;i++){
if(i==1){//表示创建第一个结点
Child ch = new Child(i);
this.firstChild=ch;//创建了第一个结点的同时,将头结点指向第一个结点
this.temp=ch;//也将跑龙套的结点指向第一个结点
}else{//继续创建新的结点
if(i==length){//表示创建的是尾结点
Child ch = new Child(i);
temp.nextChild=ch;
temp.nextChild.priorChild=temp;
temp=ch;
temp.nextChild=this.firstChild;
this.firstChild.priorChild=temp;
}else{
Child ch = new Child(i);
temp.nextChild=ch;
temp.nextChild.priorChild=temp;
temp=ch;
}
}
}
}
public void play(){//开始玩游戏
temp = this.firstChild;
//先找到开始数数的那个人
for(int i=1;i<k;i++){
temp=temp.nextChild;
}
while(length!=1){
//数m下
for(int j=1;j<m;j++){
temp=temp.nextChild;
}
System.out.println("当前出局的小孩是:"+temp.no);
temp.priorChild.nextChild=temp.nextChild;
temp.nextChild.priorChild=temp.priorChild;
temp=temp.nextChild;
this.length--;
}
System.out.println("最后出局的小孩是:"+temp.no);
}
public void show(){
//定义一个跑龙套的结点
temp = this.firstChild;
do{
System.out.print(" "+temp.no);
temp = temp.nextChild;
}while(temp!=this.firstChild);
System.out.println();
}
}
复制代码
附上测试结果:
1 2 3 4 5 6 7 8 9 10
当前出局的小孩是:3
当前出局的小孩是:5
当前出局的小孩是:7
当前出局的小孩是:9
当前出局的小孩是:1
当前出局的小孩是:4
当前出局的小孩是:8
当前出局的小孩是:2
当前出局的小孩是:10
最后出局的小孩是:6
作者:
神之梦
时间:
2013-6-12 11:55
其实不懂丢手绢游戏是怎么玩的
作者:
孙百鑫
时间:
2013-6-12 11:59
楼主应该写一下需求更好啦就好啦
作者:
麦子
时间:
2013-6-12 12:03
孙百鑫 发表于 2013-6-12 11:59
楼主应该写一下需求更好啦就好啦
就是小朋友玩得丢手绢问题,用程序来模拟这个过程
作者:
月在青天
时间:
2013-6-12 16:15
韩顺平老师java基础视频里面有,不过不是用的双向链表
作者:
月在青天
时间:
2013-6-12 16:18
package MyPro;
public class Shoupa
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
CycLink cyclink=new CycLink();
cyclink.setLen(6);
cyclink.createLink();
cyclink.setK(4);
cyclink.setM(3);
cyclink.show();
cyclink.play();
}
}
//孩子节点
class Child
{
int no;
Child nextChild=null;
public Child(int no)
{
this.no=no;
}
}
//环形链表
class CycLink
{
Child firstChild=null;
Child temp=null;
int len=0;
int k=0;
int m=0;
//设置链表大小
public void setLen(int len)
{
this.len=len;
}
//设置第k人开始数数
public void setK(int k)
{
this.k=k;
}
//设置数m下
public void setM(int m)
{
this.m=m;
}
//初始化环形链表
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.nextChild=ch;
temp=ch;
temp.nextChild=this.firstChild;
}
else
{
Child ch=new Child(i);
temp.nextChild=ch;
temp=ch;
}
}
}
}
//打印该环形链表
public void show()
{
Child temp=this.firstChild;
System.out.print("当前做游戏的小孩子是:");
do
{
System.out.print(temp.no+" ");
temp=temp.nextChild;
}while(temp!=this.firstChild);
System.out.println();
}
//执行游戏
public void play()
{
//招开始数数的人
Child temp=this.firstChild;
Child temp2=this.firstChild;//用来找到出圈的前一个小孩
for(int i=1;i<k;i++)
{
temp=temp.nextChild;
}
while(this.len!=1)
{
//数m下
for(int j=1;j<m;j++)
{ if(j!=m-1)
{temp=temp.nextChild;}
else
{
temp2=temp;//出圈的前一个 小孩
temp=temp.nextChild;
}
}
temp2.nextChild=temp.nextChild;//第m个小孩退出圈
temp=temp.nextChild;
this.len--;
}
System.out.println("最后剩下的那个孩子是:"+temp.no);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2