[题目描述]
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
[解题思路]
简单模拟,顺序枚举+判断即可。
[Code]
{
ID: zane2951
PROG: runround
LANG: PASCAL
}
program runround;
var
v:array[0..11] of boolean;
n,i,x,ce:longint;
ko:boolean;
//---------sure------------
function sure(len:longint):boolean;
var
i:longint;
begin
for i:=1 to len do if not v[i] then exit(false);
exit(true);
end;
//---------check-----------
function check(x:longint):boolean;
var
s:string;
len,now:longint;
begin
str(x,s);
len:=length(s);
now:=1;
while true do
begin
v[now]:=true;
now:=(now+ord(s[now])-ord('0')) mod len;
if now=0 then now:=len;
if v[now] and (now<>1) then exit(false);
if now=1 then
if sure(len) then exit(true) else exit(false);
end;
end;
//----------main-----------
begin
assign(input,'runround.in'); reset(input);
assign(output,'runround.out'); rewrite(output);
readln(n);
for i:=n+1 to maxlongint do
begin
ko:=true;
fillchar(v,sizeof(v),0);
x:=i;
while x>0 do
begin
ce:=x mod 10;
if v[ce] then begin ko:=false; break; end else v[ce]:=true;
x:=x div 10;
end;
fillchar(v,sizeof(v),0);
if ko then
if check(i) then begin writeln(i); break; end;
end;
close(input); close(output);
end.