Runaround Numbers循环数
循环数是那些不包括 0 这个数字的没有重复数字的整数 (比如说, 81362) 并且同时具有一个有趣
的性质, 就像这个例子:
如果你从最左边的数字开始 ( 在这个例子中是 8) 数最左边这个数字个数字到右边(回到最左边
如果数到了最右边),你会停止在另一个新的数字(如果没有停在一个不同的数字上,这个数就不是
循环数). 就像: 8 1 3 6 2 从最左边接下去数 8 个数字: 1 3 6 2 8 1 3 6 所以下一个数字是 6.
重复这样做 (这次从“6”开始数 6 个数字) 并且你会停止在一个新的数字上: 2 8 1 3 6 2, 也就
是 2.
再这样做 (这次数两个): 8 1
再一次 (这次一个): 3
又一次: 6 2 8 这是你回到了起点, 在从每一个数字开始数 1 次之后. 如果你在从每一个数字开始
数一次以后没有回到起点, 你的数字不是一个循环数.
给你一个数字 M (在1到9 位之间), 找出第一个比 M 大的循环数, 并且一定能用一个无符号长整
形数装下.
PROGRAM NAME: runround
INPUT FORMAT
仅仅一行, 包括 M
SAMPLE INPUT (file runround.in)
81361
OUTPUT FORMAT
仅仅一行,包括第一个比 M 大的循环数.
SAMPLE OUTPUT (file runround.out)
81362
================================
要注意很多的细节
1.数字全多要不同
2.数字中不包括0
3.最后一步停在1号位上..
===================
{ID:jie19952
PROG:runround
LANG:PASCAL
}
var
m:longint;
a:array[0..9]of longint;
f,f_num:array[0..9]of boolean;
a_t:longint;
procedure init;
begin
assign(input,'runround.in');
assign(output,'runround.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
function pd:boolean;
var
i:longint;
now:longint;
tot:longint;
begin
for i:=0 to a_t do f[i]:=true;
now:=1; tot:=0;
repeat
if f[now] then
begin
f[now]:=false;
now:=(now+a[now])mod a_t;
inc(tot);
end
else
begin
if (tot=a_t)and(now=1) then exit(true);
exit(false);
end;
until 1=2;
end;
function pd1:boolean;
var
i:longint;
begin
pd1:=true;
fillchar(f_num,sizeof(f_num),true);
for i:=1 to a_t do
if not f_num[a[i]] then exit(false)
else f_num[a[i]]:=false;
if not f_num[0] then exit(false);
end;
procedure main;
var
a_s:string;
i:longint;
begin
readln(m);
//m:=m;
repeat
inc(m);
str(m,a_s);
a_t:=length(a_s);
for i:=1 to a_t do a[i]:=ord(a_s[i])-ord('0');
a[0]:=a[a_t];
//writeln(a[0]);
if pd1 then
if pd then
begin
writeln(m);
terminate;
end;
until 1=2;
end;
begin
init;
main;
terminate;
end.