O
optn3
Unregistered / Unconfirmed
GUEST, unregistred user!
function TForm1.StrPos(Str:string;p:integer):string;
var
i,n:integer;
s:string;
begin
n:=0;
for i:=0 to length(str) do
begin
if copy(str,i,length(' '))=' ' then n:=n+1;
if n = p then
s := copy(str,i+2,2);
end;
StrPos:=s;
end;
function TForm1.StrPostime(Str:TStringList;P:string):integer;
var i,n:integer;
begin
n:=0;
for i:=0 to str.Count -1 do
if pos(p,str.Strings)>0 then n := n+1;
StrPostime:=n;
end;
procedure vn;
var
j,i:integer;
List :TStringList;
st1,p,m:string;
Lists: TStringList;
begin
Lists:= TStringList.Create;
randomize;
st1:='';
p:=' 02 07 24 26 32 05 17 25 28 33'; //从这里随机抽5个数.不能重复.
j:=0;
while j < 5 do //我认为慢在这里.如果能取消循环,直接取,
begin
i:= random(10)+1;
m:= form1.StrPos(p,i);
if form1.StrPostime(lists ,m) = 0 then
begin
st1 := st1+ ' '+form1.StrPos(p,i);
lists.Add(m);
j:=j+1;
end;
end;
Lists.Clear;
Lists.Free;
form1.memo1.Lines.Add(st1);
end;
上述代码本意,从字符串P中随机抽5个数.列入MEMO1
为什么老觉得慢慢,请教怎样提高代码速度.好的算法.可否贴出来.
var
i,n:integer;
s:string;
begin
n:=0;
for i:=0 to length(str) do
begin
if copy(str,i,length(' '))=' ' then n:=n+1;
if n = p then
s := copy(str,i+2,2);
end;
StrPos:=s;
end;
function TForm1.StrPostime(Str:TStringList;P:string):integer;
var i,n:integer;
begin
n:=0;
for i:=0 to str.Count -1 do
if pos(p,str.Strings)>0 then n := n+1;
StrPostime:=n;
end;
procedure vn;
var
j,i:integer;
List :TStringList;
st1,p,m:string;
Lists: TStringList;
begin
Lists:= TStringList.Create;
randomize;
st1:='';
p:=' 02 07 24 26 32 05 17 25 28 33'; //从这里随机抽5个数.不能重复.
j:=0;
while j < 5 do //我认为慢在这里.如果能取消循环,直接取,
begin
i:= random(10)+1;
m:= form1.StrPos(p,i);
if form1.StrPostime(lists ,m) = 0 then
begin
st1 := st1+ ' '+form1.StrPos(p,i);
lists.Add(m);
j:=j+1;
end;
end;
Lists.Clear;
Lists.Free;
form1.memo1.Lines.Add(st1);
end;
上述代码本意,从字符串P中随机抽5个数.列入MEMO1
为什么老觉得慢慢,请教怎样提高代码速度.好的算法.可否贴出来.