資料的累積 |
尚未結案
|
cherng32
一般會員 發表:26 回覆:24 積分:10 註冊:2002-09-18 發送簡訊給我 |
大家好:
因為要作一個產生亂數且排序的程式,而遇到了一些問題,因此想請問一下大家,謝謝!! 程式一:產生亂數
SetLength(aSelect ,a); // 設定陣列30個元素 樣本數
Randomize;
For i:=0 To a-1 Do Begin
While True Do Begin
t := Random(c-b 1) b; // 隨機值的範圍
If i = 0 THen Break; //
Dupl :=False; //
For j:= i-1 DownTo 0 Do Begin // 往前檢查是否有重複的值
If aSelect[j]=t Then Begin
Dupl := True; // 已有相同的值
Break ; // 不再檢查,準備取下一個隨機值
End;
End;
If Not Dupl Then Break; // 沒有重複,可加入到陣列了
End; memo2.lines.add(inttostr(t)); 程式二:進行排序
function MySortAsc(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := CompareValue(StrtoInt(List[Index1]),StrToInt(List[Index2]));
end; function MySortDesc(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := -MySortAsc(List,Index1,Index2);
end; try
s.CommaText := '2,-10,20,-40,1,50,7,-12,11';
s.Sorted := False;
s.CustomSort(MySortAsc); // 由小到大
showmessage(s.CommaText);
memo1.Text:=s.CommaText;
s.CustomSort(MySortDesc); // 由大到小
showmessage(s.CommaText);
finally
s.Free;
end;
但是我遇到的問題是在s.CommaText := '2,-10,20,-40,1,50,7,-12,11';這行中無法讓程式一t所產生的亂數不段的累積到s.CommaText中??
永遠在學習的新手
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi cherng32, 改成以下程式碼試試,看看是否符合你的需要
try s.CommaText := Memo1.Text ',' '2,-10,20,-40,1,50,7,-12,11'; s.Sorted := False; s.CustomSort(MySortAsc); showmessage(s.CommaText); Memo1.Lines.CommaText:=s.CommaText; s.CustomSort(MySortDesc); showmessage(s.CommaText); finally s.Free; end;---------------------------------- 小弟才疏學淺,若有謬誤尚請不吝指教 ----------------------------------
------
Fishman |
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
cherng32 您好: 由程式看起來,您是把新產生的隨機數放入memo2中。
所以
s.CommaText := '2,-10,20,-40,1,50,7,-12,11';
似乎要修改成,
s.AddStrings( memo2.Lines );
這樣就能把新產生的值加入了。
不過不精確知道您的s是如何產生的,可能您得再查查看。 另外您的程式一的部分有一個小缺失:沒有把第一個隨機產生的值放入aSelect中。
If i = 0 THen Break; // original code -------------------- If i = 0 THen begin aSelect[0] := t; Break; endRichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====##### |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |