package DataStructureTestSinglyLinkedList;
public class Josephus {
public Josephus(int n, int s , int d)
{
SinglyLinkedList<String> list = new SinglyLinkedList<String>();
for(int j=0;j<n;j++)
list.append( (char)('A'+j)+"");
System.out.print("约瑟夫环"+"("+n+","+s+","+d+")");
System.out.println(list.toString());
int i=s;
while(list.length()>1)
{
i=(i+d-1)%list.length();
System.out.print("删除"+list.remove(i).toString());
System.out.println(list.toString());
}
System.out.println("幸存者是:"+list.toString());
}
public static void main(String[] args)
{
new Josephus(5,0,2);
}
}
单链表进阶——编写一个约瑟夫环
最新推荐文章于 2024-05-05 21:32:17 发布