Josephu问题实现

package com.acm;

import java.util.ArrayList;
import java.util.List;

import com.bean.Node;

public class Josephu {
public static void generate(){
int totalP = 41;
int dead = 3;

List list = new ArrayList();
for(int i = 2;i <= totalP;i++){
if( i == 16){
list.add("Josephu's friend " + i);
}else if(i == 31){
list.add("Josephu " + i);
}else{
list.add("" + i);
}
}

Node head = new Node("1");

SingleCycleLinkedList scll = new SingleCycleLinkedList(head, list);
Node current = head;
while(scll.size(current) > 3){
for(int i = 0;i < dead - 2;i++){
current = current.getNext();
}
current.setNext(current.getNext().getNext());
current = current.getNext();
}

print(current);
}

public static void print(Node head){
Node current = head;

do{
System.out.println(current.getData());
current = current.getNext();
}while(current.getNext() != head);
}

public static void main(String[] args) {
generate();
}
}

你可能感兴趣的:(java)