求各位大虾帮忙调试程序!(50分)

  • 主题发起人 qifenglin
  • 开始时间
Q

qifenglin

Unregistered / Unconfirmed
GUEST, unregistred user!
算法:从n个数中取m个不重复的数。
我的程序:
function GetRndNum(const Amount,UpLimit:Integer):TStringList;
var
RanSeeds:array of integer;
IsOK:boolean;
i:integer;
TempNum,K:LongInt;
//GetStr:string;
begin
result:=TStringList.Create;
SetLength(RanSeeds,Amount-1);
Randomize;

//获得第一个不会重复的数字
RanSeeds[0]:=trunc((UpLimit) * random) + 1;
result.Add(intToStr(RanSeeds[0]));
i:=1;
while i<=Amount-1 do //从第二个数开始获得;
begin
Randomize;
TempNum:=trunc((UpLimit) * Random) + 1; //先获得一个随机数
IsOK:=true;
for K:=0 to i-1 do
begin
if TempNum = RanSeeds[K] then //判断该数与以前的数是否重复,如果重复的话
begin
IsOK:=False;
end;
end;
if IsOK then
begin
RanSeeds:=TempNum; //如果没有重复的则存入数组
result.Add(IntToStr(RanSeeds));
i:=i+1;
end;
end;
RanSeeds:=Nil;
end;

调用:
procedure TForm1.Button1Click(Sender: TObject);
var
GetValue:TstringList;
i:integer;
b:string;
begin
GetValue:=GetRndNum(4,5);
for i:=0 to GetValue.Count-1 do
b:=b+GetValue.Strings+';';
showmessage(b);
GetValue.free;
end;
出现的问题:invalid pointer Operation。
调试时有时能得出结果,有时报以上错误。开发环境:D6
 
为什么要RanSeeds:=Nil;?
 
动态数组释放空间。我怀疑是数组的问题,所以特意加了这一句。
 
to zw84611:
谢谢你帮我解决了有关RichEdit非编辑状态的问题。
 
对不起,我已经调试出来了。动态分配数组时应该是SetLength(RanSeeds,Amount);而不是SetLength(RanSeeds,Amount-1);。谢谢各位了。
 
顶部