約瑟夫環(huán)問題:一圈共有N個(gè)人净捅,開始報(bào)數(shù)圈浇,報(bào)到M的人自殺,然后重新開始報(bào)數(shù)您旁,問最后自殺的人是誰烙常?
//鏈表實(shí)現(xiàn)
public class Node{
public int value;
public Node next;
public Node(int data){
this.value=data;
}
}
public Node josephuskill(Node head,int m){
if(head==null||head.next==head||m<1){
return head;
}
Node last=head;
while(last.next!=head){
last=last.next;
}
int count=0;
while(head!=last){
if(++count==m){
last.next=head.next;
count=0;
}
else{
last=last.next;
}
head=last.next;
}
return head;
}