xiaozqh
[srcbox.git] / hex.pas
blobb136df2946d5b74a4b0b67c104ea02c84a573852
1 var
2 i,n,k:longint;
3 hex:string;
5 procedure jian(var hex:string; k:longint);
6 var
7 i,j:longint;
8 s:set of char;
10 begin
11 s:=[];
12 for i:=1 to k-1 do s:=s+[hex[i]];
13 for i:=k to length(hex) do
14 begin
15 if i>k then hex[i]:=succ('F');
16 repeat
17 hex[i]:=pred(hex[i]);
18 if hex[i]=pred('A') then hex[i]:='9';
19 until not(hex[i] in s);
20 s:=s+[hex[i]];
21 end;
22 end;
24 begin
25 readln(n);
26 hex:='FEDCBA98';
27 while n>1 do
28 begin
29 k:=1;
30 for i:=1 to length(hex)-1 do k:=k*(16-i);
31 i:=1;
32 repeat
33 if n>k then break;
34 k:=k div (16-i);
35 inc(i);
36 until false;
37 jian(hex,i);
38 dec(n,k);
39 if hex[1]='0' then delete(hex,1,1);
40 end;
41 writeln(hex);
42 end.