安琪儿公司位置:求助:是关于PASCAL的约瑟夫问题的

来源:百度文库 编辑:高校问答 时间:2024/04/29 11:47:33
....
repeat
if (n+b) mod m=0 then b:=m else b:=(n+b)mod m; //算出哪一个人被T
for a:=b to m-1 do shu[a]:=shu[a+1]; //把后边的人往前移一位
m:=m-1; //人数-1
until m=1;
...
这里面的'if (n+b) mod m=0 then b:=m else b:=(n+b)mod m;'我看不懂,
可不可以解释一下~~~hehe

var a:array[1..10000]of integer;
m,n,i,j,s:integer;
begin
readln(m,n);for i:=1 to m do a[i]:=1;
i:=0;s:=0;j:=0;
repeat
i:=i+1;if i>m then i:=1;if a[i]=1 then begin s:=s+1;
if s=n then begin a[i]:=0; writeln(i);j:=j+1;s:=0;
end;end;
until j=m
end.

end.

如果n+b能被m整除,则b为m
否则,b赋值为(n+b)除以m的余数