请高手看看我这段代码问题之处!!!!(100分)

  • 主题发起人 主题发起人 optn3
  • 开始时间 开始时间
O

optn3

Unregistered / Unconfirmed
GUEST, unregistred user!
代码意为:从X到Y范围内,随机选z个不相同的数.spinedit1为x,spinedit2为y,spinedit3为z.如果从:1-10000中,选25个数.我这里选到的最大数都在3000以下,为什么?<br>代码有点垃圾,望高手指点指点?提高速度.多谢.<br><br>procedure TForm1.rdmselect(min,all, want: integer; var got: array of integer); &nbsp; &nbsp;//随机过程......<br>//all:题目总数<br>//want:取多少个<br>//got:存放结果<br>var<br>rdmarray,sortarray:array of integer;//动态数组<br>i,j,temp:integer;//整形变量<br>begin<br>setlength(rdmarray,all);//设定动态数组大小<br>setlength(sortarray,all);<br>randomize;//初始化随即种子<br><br>for i:= 1 to all do<br>&nbsp; begin<br>&nbsp; &nbsp; rdmarray[i-1]:= random(100);<br>&nbsp; &nbsp; sortarray[i-1]:=i+min;//该数组值为1、2。。。all<br><br>&nbsp; end;<br><br><br>for i:=0 to all-2 do//冒泡排序,换成选择更好。目的:按照随机数组大小排序,同时标志数组也随之改变,最终标志数组为随机数组按大小排序的“名次”<br>&nbsp; for j:=i+1 to all-1 do<br>&nbsp; &nbsp; if rdmarray&gt;rdmarray[j] then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; temp:=rdmarray;<br>&nbsp; &nbsp; &nbsp; &nbsp; rdmarray:=rdmarray[j];<br>&nbsp; &nbsp; &nbsp; &nbsp; rdmarray[j]:=temp;<br>&nbsp; &nbsp; &nbsp; &nbsp; temp:=sortarray;<br>&nbsp; &nbsp; &nbsp; &nbsp; sortarray:=sortarray[j];<br>&nbsp; &nbsp; &nbsp; &nbsp; sortarray[j]:=temp;<br>&nbsp; &nbsp; &nbsp; end;<br><br>for i:=0 to want-1 do//要几个取几个<br>&nbsp; got:=sortarray;<br><br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject); &nbsp; &nbsp; &nbsp;//随机<br>var<br>a:array of integer;<br>i:integer;<br>sl1 : TStrings;<br>begin<br>&nbsp;sl1 := TStringList.Create;<br>&nbsp;setlength(a,spinedit3.Value);//edtwant:输入:取出多少个<br>&nbsp;rdmselect(spinedit1.Value-1,spinedit2.Value-spinedit1.Value+1,spinedit3.Value,a);//edtall:输入:总共多少个<br>&nbsp;memo1.Lines.Clear;//存放结果<br><br>&nbsp;for i:=low(a) to high(a) do<br>&nbsp; &nbsp; sl1.Add(inttostr(a));<br>&nbsp; memo1.Text := sl1.Text;<br>&nbsp; sl1.Clear;<br>&nbsp; sl1.Free;<br>end;
 
for i:= 1 to all do<br>&nbsp; begin<br>&nbsp; &nbsp; rdmarray[i-1]:= random(100); {这里有问题吧,random的参数是取值的最大值,你给了100也就是说随即最大数是100}<br>&nbsp; &nbsp; sortarray[i-1]:=i+min;//该数组值为1、2。。。all<br><br>&nbsp; end;
 
to hs-kill<br>rdmarray[i-1]:= random(100); 如果改成rdmarray[i-1]:= random(all-min);行吗?
 
应该可以......
 
多谢 &nbsp;hs-kill
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
772
import
I
后退
顶部