窮舉密碼算法,請指教(50分)

T

tihao

Unregistered / Unconfirmed
GUEST, unregistred user!
我想寫一段窮舉密碼的代碼
怎麼寫啊??
就是得到一密碼字符串,這字符串是這樣變化的
'0'
'1'
...
'z'
'00'
'01'
'02'
...
'zz'
'000'
......

請大俠幫忙啊!

 
你有另一贴吧!


procedure createpassword;
const
passwordmax=8;
a:pchar='0123456789abcdefghijklmnopqrstuvwxyz';
var
n,i,j,nmin,nmax:integer;
cstdiofile:textfile;
nn:array[1..passwordmax] of integer;
s:string;
isend:boolean;
begin
n:=length(a);
nmin:=1;
nmax:=3;
IF (nmin>nmax) OR (nmax>passwordmax) then raise exception.Create('超界');

try
AssignFile(cstdiofile, 'c:/dict.txt');
Rewrite(cstdiofile);
for i:=nmin to nmax do
begin
for j:=1 to i do nn[j]:=0;
isend:=false;
while true do
begin
s:='';
for j:=1 to i do s:=s+a[nn[j]];
writeln(cstdiofile,s);
j:=i;
while true do
begin
inc(nn[j]);
if (j=1) and (nn[j]=n) then
begin
isend:=true;
break;
end;
if (nn[j]=n) then
begin
nn[j]:=0;
dec(j);
continue;
end;
break;
end;
if isend then break;
end;
end;
finally
closeFile(cstdiofile);
end;
end;
 
用递归比较好看懂,调用Pass(n)就会生成n位长的所有密码
function Pass(len:integer):TStringList;
const
a:pchar='0123456789abcdefghijklmnopqrstuvwxyz';
var
list1,list2:TStringList;
i,j:integer;
begin
if len<=0 then
begin
result:=nil;
exit;
end;
list1:=TStringList.Create;
list2:=Pass(len-1);
if list2=nil
then for i:=1 to length(a) do list1.Add(a)
else begin
for i:=1 to length(a) do
for j:=0 to list2.Count-1 do
list1.Add(a+list2[j]);
list2.Free;
end;
result:=list1;
end;
 
jsxjd我已經測試,不錯!!
妙兔无花我還沒測試!
 
多人接受答案了。
 
顶部