Josephus问题

public class Link {
    public long lData;
    public Link next;

    public Link(long lData){
        this.lData=lData;
    }

    public void displayLink(){
        System.out.print(lData+" ");
    }
}
public class CircleList {
    private Link current;
    private int nItems;

    public CircleList(){
        current=null;
    }

    public void insert(long value){
        Link newLink = new Link(value);
        if(current==null){
            current=newLink;
            newLink.next=newLink;
        }else{
            newLink.next=current.next;
            current.next=newLink;
            current=newLink;//插入元素,current要移动要新元素
        }
        nItems++;
    }

    public long remove(){
        long temp = current.next.lData;
        if(current.next==current){
            current=null;
        }else{
            current.next=current.next.next;
        }
        nItems--;
        return temp;
    }

    public long peek(){
        return current.next.lData;
    }

    public Link find(long value){
        Link temp =current;
        Link result=null;
        if(current==null){
            return result;
        }
        do{
            step();//从current的下一个元素开始
            if(current.lData==value){
                result=current;
                current=temp;//还原current到原来的位置,这样就不会打乱插入的顺序,current指向最后插入的元素
            }
        }while(current!=temp);//current到原来的位置,一周循环结束
        return result;
    }

    public void step(){
        if(current!=null){
            current=current.next;// 调用step()方法后,顺序会被打乱
        }
    }

    public void display(){
        if(current!=null){
            Link temp =current;
            do{
                step();// 从current的一下个开始显示
                System.out.print(current.lData + " ");
            }while(current!=temp);
        }
        System.out.println();
    }

    public boolean isEmpty(){
        return (current==null);
    }

    public int size(){
        return nItems;
    }
}
public class Josephus {
    /**
     * @param args
     */
    public static long getJosephus(int number){
        CircleList theCircleList = new CircleList();
        for(int i=1;i<=number;i++){
            theCircleList.insert(i);
        }
        System.out.print("原始圈子:");
        theCircleList.display();
        System.out.print("出圈顺序:");
        for(int i=1;i<number;i++){//要删掉number-1个人
            for(int j=1;j<4;j++){//不能数到4,remove删掉下一个
                theCircleList.step();
            }
            System.out.print(theCircleList.remove()+" ");
        }
        System.out.println();
        return theCircleList.peek();
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long number = Josephus.getJosephus(7);
        System.out.println("Josephus的编号是:"+number);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值