狐狸捉兔子
围绕着山顶有10个洞,一只狐狸和一只兔子住在各自的洞里。狐狸总想吃掉兔子。一天,兔子对狐狸说:“你想吃我有一个条件,先把洞从1~10编上号,你从10号洞出发,先到1号洞找我;第二次隔1个洞找我,第三次隔2个洞找我,以后依此类推,次数不限。若能找到我,你就可以饱餐一顿。不过在没有找到我以前不能停下来。”狐狸满口答应就开始找了,它从早到晚进了1000次洞,累得昏了过去也没找到兔子。请问,兔子躲在几号洞里?
program mr;
const
holenumber = 10;
var
hole : array[1 .. holenumber] of 0 .. 1;
step, i, number : longint;
begin
for i := 1 to holenumber do
hole[i] := 0;
number := 0;
for step := 1 to 1000 do
begin
number := number+step; {循环地数}
i := number mod holenumber; {第几个洞}
if i = 0 then i := 10;
hole[i] := 1;
end;
for i := 1 to holenumber do
if hole[i] = 0 then
write(i:3);
readln;
end.
program mr;
const
holenumber = 10;
var
hole : array[1 .. holenumber] of 0 .. 1;
step, i, number : longint;
begin
for i := 1 to holenumber do
hole[i] := 0;
number := 0;
for step := 1 to 1000 do
begin
number := number+step; {循环地数}
i := number mod holenumber; {第几个洞}
if i = 0 then i := 10;
hole[i] := 1;
end;
for i := 1 to holenumber do
if hole[i] = 0 then
write(i:3);
readln;
end.
N个人围成一圈玩游戏,按1-N编号,从1号开始,按顺时针方向数数,数到M的出队,当剩下最后一个人时,这个人就是游戏的胜利者,那么,请问,你应该站在什么位置,才能成为胜利者?
输入:N M(均小于1000)
输出:胜利者的位置。
program JOSEPHUS;
var n, m, s, f, t : integer;
a : array[1..1000] of boolean;
begin
writeln('input n, m');
readln(n, m);
f := 0;
t := 0;
s := 0;
repeat
t := t+1;
if t = n+1 then t := 1;
if a[t] = false then s := s+1;
if s = m then
begin
s := 0;
a[t] := true;
f := f+1;
end;
until f = n-1;
for t := 1 to n do
if a[t] = false then
begin
writeln(t);
break;
end;
readln;
end.
输入:N M(均小于1000)
输出:胜利者的位置。
program JOSEPHUS;
var n, m, s, f, t : integer;
a : array[1..1000] of boolean;
begin
writeln('input n, m');
readln(n, m);
f := 0;
t := 0;
s := 0;
repeat
t := t+1;
if t = n+1 then t := 1;
if a[t] = false then s := s+1;
if s = m then
begin
s := 0;
a[t] := true;
f := f+1;
end;
until f = n-1;
for t := 1 to n do
if a[t] = false then
begin
writeln(t);
break;
end;
readln;
end.