首页 > 代码库 > 标准约瑟夫问题链表版

标准约瑟夫问题链表版

type
    point=^node;
    node=record
        data:longint;
        next:point;
    end;
var
    h,s,p,q:point;
    m,n,i,t:longint;
begin
    readln(n,m);

    //create the linked list
    new(s);  h:=s;  s^.data :=1;  p:=s;
    for i:=2 to n do
    begin
        new(s);  s^.data:=i;  p^.next:=s;  p:=s;
    end;
    p^.next:=h;

    //elect the king
    q:=p; t:=0;
    repeat
        p:=q^.next;  inc(t);
        if t=m then
        begin
            q^.next:=p^.next;  writeln(p^.data); dispose(p);  t:=0
        end
        else q:=p;
    until p=p^.next;

    writeln(p^.data);
end.

标准约瑟夫问题链表版