-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunround.pas
77 lines (69 loc) · 1.18 KB
/
runround.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{
ID:10179811
PROG:runround
LANG:PASCAL
#FILE
}
type
arr=array[1..10] of longint;
var
flag:array[1..9] of boolean;
m,temp,min:qword;
function test(a:arr;k:longint):boolean; //判断循环数的条件之一就是全都走一遍
var
i:longint;
begin
for i:=1 to k do
if a[i]<>1 then exit(false);
exit(true);
end;
function judge(n:qword):boolean; //判断是不是循环数
var
a:arr;
i,k:longint;
s:string;
begin
fillchar(a,sizeof(a),0);
i:=1;
str(n,s);
k:=length(s);
repeat
i:=ord(s[i])-ord('0')+i;
i:=((i-1) mod k)+1;
inc(a[i]);
if a[i]>1 then exit(false);
until test(a,k);
exit(true);
end;
procedure dfs; //直接深搜
var
j:longint;
begin
if temp>m then
begin
if judge(temp) and (min>temp) then min:=temp;
exit;
end;
for j:=1 to 9 do
if flag[j] then
begin
flag[j]:=false;
temp:=temp*10+j;
dfs;
flag[j]:=true;
temp:=temp div 10;
end;
end;
begin
assign(input,'runround.in');
reset(input);
assign(output,'runround.out');
rewrite(output);
readln(m);
min:=maxlongint;
fillchar(flag,sizeof(flag),true);
dfs;
writeln(min);
close(input);
close(output);
end.