/**
*
*/
package com.tjb.test;
/**
* @author tjbworkshop
*
*/
import java.util.*;
public class Josephus{
private List beginList = null;
private int total = 0;
private int pass = 0;
private int rest = 1;
public List getBeginList() {
return beginList;
}
public void setBeginList(List beginList) {
this.beginList = beginList;
}
public int getPass() {
return pass;
}
public void setPass(int pass) {
this.pass = pass;
}
public int getTotal() {
return total;
}
public int getRest() {
return rest;
}
public void setRest(int rest) {
this.rest = rest;
}
public void setTotal(int total) {
this.total = total;
}
@SuppressWarnings("unchecked")
public Josephus(int total,int pass)
{
this.total = total;
this.pass = pass;
this.beginList = new LinkedList();
for(int i = 1;i<=total;i++)
{
beginList.add(new Integer(i));
}
}
public List jos()
{
Iterator it = getBeginList().iterator();
while(total--!=getRest())
{
for(int i = 1;i<=getPass();i++)
{
if(!it.hasNext())
it = beginList.iterator();
System.out.println("游标指向:"+it.next());/*it.next()必须执行*/
}
it.remove();
}
return getBeginList();
}
public static void main(String[] args){
Josephus jos = new Josephus(4,2);
jos.setRest(1);
System.out.println(jos.jos().get(0));
}
}