此题很简单,搜索题。
按照步骤老老实实的做就能过,不过这题要注意要用int64,高精也可以。虽然题目要求的是说maxlongint以内,但是由于倒过来就不只maxlongint了。
另:记得补0和自己本身有可能是循环节。
代码如下:
var n,k,r:int64;
g,i,j,l:longint;
a:array[0..10000]of int64;
over:boolean;
t:string;
num:array[0..9]of longint;
procedure make(g:longint;n:int64;var a,b:int64);
var s:string;i,j:longint;z:int64;
begin
fillchar(num,sizeof(num),0);
str(n,s);
for i:=1 to length(s) do
begin
inc(num[ord(s[i])-ord('0')]);
end;
z:=1;
a:=0;
i:=0;
for i:=0 to 9 do
begin
if num[i]<>0 then
begin
for j:=1 to num[i] do
begin
a:=a+i*z;
z:=z*10;
end;
end;
end;
z:=1;
b:=0;
for i:=9 downto 0 do
begin
if num[i]<>0 then
begin
for j:=1 to num[i] do
begin
dec(num[i]);
b:=b+i*z;
z:=z*10;
end;
end;
end;
end;
begin
while not(eof) do
begin
readln(n);
str(n,t);
g:=length(t);
make(g,n,k,r);
r:=k-r;
a[1]:=n;
l:=2;
a[2]:=r;
over:=false;
while not(over) do
begin
make(g,a[l],k,r);
inc(l);
a[l]:=k-r;
for i:=l-1 downto 1 do
begin
if a[l]=a[i] then
begin
for j:=i to l-1 do write(a[j],' ');
writeln;
over:=true;
break;
end;
end;
end;
end;
end.
g,i,j,l:longint;
a:array[0..10000]of int64;
over:boolean;
t:string;
num:array[0..9]of longint;
procedure make(g:longint;n:int64;var a,b:int64);
var s:string;i,j:longint;z:int64;
begin
fillchar(num,sizeof(num),0);
str(n,s);
for i:=1 to length(s) do
begin
inc(num[ord(s[i])-ord('0')]);
end;
z:=1;
a:=0;
i:=0;
for i:=0 to 9 do
begin
if num[i]<>0 then
begin
for j:=1 to num[i] do
begin
a:=a+i*z;
z:=z*10;
end;
end;
end;
z:=1;
b:=0;
for i:=9 downto 0 do
begin
if num[i]<>0 then
begin
for j:=1 to num[i] do
begin
dec(num[i]);
b:=b+i*z;
z:=z*10;
end;
end;
end;
end;
begin
while not(eof) do
begin
readln(n);
str(n,t);
g:=length(t);
make(g,n,k,r);
r:=k-r;
a[1]:=n;
l:=2;
a[2]:=r;
over:=false;
while not(over) do
begin
make(g,a[l],k,r);
inc(l);
a[l]:=k-r;
for i:=l-1 downto 1 do
begin
if a[l]=a[i] then
begin
for j:=i to l-1 do write(a[j],' ');
writeln;
over:=true;
break;
end;
end;
end;
end;
end.