算法:模拟
又是一道模拟题,找离给出的数最近的循环数。
又是一道模拟题,找离给出的数最近的循环数。
因为不能出现0而且不能重复,所以我们转换成字符串然后开一个bb数组记录一下,判断的时候也用一个b数组记录一下,把走过的位置标记一下,如果找到某个位置已走过,查看是不是起点,若不是就退出再往下试。
{
ID:1011mashuo
PROG:runround
LANG:PASCAL
}
program runround;
var
m:longint;
ch:array [0..8] of longint;
b:array [0..8] of boolean;
bb:array [1..9] of boolean;
function test(x:longint):boolean;
var
i,j,t,n,start:longint;
s:string;
begin
fillchar(ch,sizeof(ch),0);
fillchar(bb,sizeof(bb),false);
str(x,s);
n:=length(s);
for i:=1 to n do
begin
ch[i-1]:=ord(s[i])-48;
if ch[i-1]=0 then exit(false);
if not bb[ch[i-1]] then bb[ch[i-1]]:=true else exit(false);
end;
for i:=0 to n-1 do
begin
fillchar(b,sizeof(b),false);
t:=i;
start:=i;
while not b[t] do
begin
b[t]:=true;
t:=(t+ch[t]) mod n;
end;
for j:=0 to n-1 do if not b[j] then exit(false);
if t<>start then exit(false);
end;
exit(true);
end;
procedure main;
begin
while 1=1 do
begin
inc(m);
if test(m) then
begin
writeln(m);
halt;
end;
end;
end;
begin
assign(input,'runround.in');
reset(input);
assign(output,'runround.out');
rewrite(output);
readln(m);
main;
close(input);
close(output);
end.