{****************************Задача Джозефуса********************************}
uses
 CRT,Graph;
type
 psold=^sold;
 sold=record
       prev,next : psold;
       num : word;
      end;
var
 point,first,p : psold;
 nsold,step : word;
 j,i,r,gdriver,gmode : integer;
 angstep : real;
 ang : ArcCoordsType;
 draw : boolean;
 numb : string;
{----------------------------------------------------------------------------}
{$i svga256.inc}
{$F+}
function DetectVGA256 : Integer;
 var
  Vid : Integer;
 begin
  repeat
   WriteLn('Which video mode would you like to use?');
   WriteLn('  1) 640x400x256');
   WriteLn('  2) 640x480x256');
   WriteLn('  3) 800x600x256');
   WriteLn('  4) 1024x768x256');
   Readln(Vid);
   if not (vid in [1..4]) then WriteLn('Повторить!');
  until (vid in [1..4]);
  DetectVGA256:=Vid;
 end;
{ $L svgadrv.obj }
{--------------------------------------------------------------------------}
begin
 WriteLn('Сколько участников? :');
 ReadLn(nsold);
 if nsold<=0 then begin WriteLn('Маловато будет!'); ReadLn; exit; end;
 WriteLn('Каков шаг (до скольки считать)? : ');
 ReadLn(step);
 if step=0 then begin WriteLn('Маловато будет!'); ReadLn; exit; end;
 gdriver:=InstallUserDriver('SVGA256',@DetectVGA256);
 gdriver:=Detect;
 InitGraph(gdriver,gmode,'');
 SetColor(RED);
 SetTextStyle(DefaultFont, HorizDir, GetMaxY div 300);
 draw:=true;
 r:=Round(pi*((10*(GetMaxy div 6)) div (6*nsold)));
 if r>GetMaxy div 15 then r:=GetMaxy div 15;
 if r<=2 then
  begin
   OutTextXY(GetMaxX div 2,GetMaxY div 2,'Too many men!');
   draw:=false; ReadLn; CloseGraph;
  end;
 if draw then
  begin
    angstep:=360/nsold; SetFillStyle(1,RED);
  end;
 New(first);
 first^.next:=first;
 first^.prev:=first;
 first^.num:=nsold;
 for i:=nsold downto 1 do
  begin
   if i<nsold then
    begin
     New(p);
     p^.prev:=first^.prev;
     p^.next:=first;
     first^.prev:=p;
     p^.prev^.next:=p;
     p^.num:=i;
    end;
   if draw then
   begin
    SetColor(BLACK); Arc(GetMaxx div 2,GetMaxy div 2,Round(i*angstep),Round((i+0.5)*angstep),5*GetMaxy div 12);
    SetColor(RED); GetArcCoords(ang); with ang do PieSlice(xstart,ystart,0,360,r);
    SetColor(BLUE); str(P^.num,numb); OutTextXY(ang.xstart,ang.ystart,numb);
   end;
  end;
 j:=nsold;
 point:=first;
 if draw then begin SetFillStyle(1,CYAN); end;
 while j>1 do
  begin
   for i:=1 to step do
    point:=point^.next;
   if draw then Delay(5000);
   point^.next^.prev:=point^.prev;
   point^.prev^.next:=point^.next;
   if draw then begin
    SetColor(BLACK);
    Arc(GetMaxx div 2,GetMaxy div 2,Round(point^.num*angstep),Round((point^.num+0.5)*angstep),5*GetMaxy div 12);
    SetColor(CYAN); GetArcCoords(ang); with ang do PieSlice(xstart,ystart,0,360,r);
   end;
   point:=point^.next;
   {if readkey then exit;}
   DEC(j);
  end;
 str(point^.num,numb);
 numb:='Number '+numb+' is alive!';
 if draw then
  begin
   SetColor(GREEN);
   OutTextXY(GetMaxX div 3,GetMaxY div 2,numb);
   ReadLn;
   CloseGraph;
  end;
 WriteLn('Остался номер ',point^.num);
 ReadLn;
end.