分配記數排序 |
缺席
|
inglong
初階會員 發表:28 回覆:27 積分:36 註冊:2004-08-13 發送簡訊給我 |
遇到一些問題:要如何配置陣列和陣列之間的對應
先說明方法:
由使用者輸入要排序的數值,大小不限
輸入到A[] 使用者輸入多少就有多少個
然後去判斷使用者輸入的資料裡,有哪些相同的數
使用count[]去紀錄===>count[ A[i] ] EX:輸入為1 2 1 2 3 1 2 3
那count[1]=3,count[2]=3,count[3]=2
然後再算出每個數之間的位置
ex:1:3
2:3 3=6
3:3 3 2=8
於是排序就出來了
i 1 2 3 4 5 6 7 8
A[i] 1 1 1 2 2 2 3 3 這樣如何用delphi實作呢
小弟失敗很多次了
有沒有範例可以參考呢
謝謝~~ 發表人 - inglong 於 2005/08/28 17:57:50
------
何需Coding爭峰 千人指 萬人鋒 敢問Coding頂峰 三尺秋水塵不染 天下無雙 |
inglong
初階會員 發表:28 回覆:27 積分:36 註冊:2004-08-13 發送簡訊給我 |
procedure TForm1.Button7Click(Sender: TObject);
var
i,c:integer;
dis,cou:array of integer;
//dis是輸入資料的陣列
//cou是計算dis列中某數的個數
begin
c:=memo3.lines.count;//取得使用者輸入陣列的大小
setlength(dis,c);
setlength(cou,100);//設定記數陣列的大小ps.使用者輸入的述範圍是0~99 for i:=0 to c-1 do
begin
dis[i]:=strtoint( memo3.lines[i] );//講使用者輸入的數傳到陣列中
end; for i:=0 to 99 do
begin
cou[i]:=0;//初值化cou陣列
end; //接下來的問題是,我要如何統計陣列中相同數字的個數?? end; 何需Coding爭峰
千人指 萬人鋒
敢問Coding頂峰
三尺秋水塵不染
天下無雙 發表人 - inglong 於 2005/08/29 12:32:40
------
何需Coding爭峰 千人指 萬人鋒 敢問Coding頂峰 三尺秋水塵不染 天下無雙 |
inglong
初階會員 發表:28 回覆:27 積分:36 註冊:2004-08-13 發送簡訊給我 |
procedure TForm1.Button7Click(Sender: TObject);
var
i,c,j:integer;
dis,cou:array of integer;
begin
c:=memo3.Lines.Count;
setlength(dis,c);
setlength(cou,100); for i:=0 to c-1 do
begin
dis[i]:=strtoint(memo3.Lines[i]);
end; for i:=0 to 99 do
begin
cou[i]:=1;
end; for i:=0 to c-1 do
begin
for j:=c-1 downto (i+1) do
begin
if (dis[i]=dis[j]) then
begin
cou[dis[i]]:=cou[dis[i]]+1;
end;
end;
end; for i:=0 to c-1 do
begin
for j:=c-1 downto i+1 do
begin
if ( dis[i]>dis[j] ) then
begin
cou[dis[i]]:=cou[dis[i]]+cou[dis[j]];
end
else
if ( dis[i]
------
何需Coding爭峰 千人指 萬人鋒 敢問Coding頂峰 三尺秋水塵不染 天下無雙 |
inglong
初階會員 發表:28 回覆:27 積分:36 註冊:2004-08-13 發送簡訊給我 |
//終於完成了@@
//ps.memo3是輸入數字後顯示的陣列
//edit2輸入數字到memo3
//陣列大小為100範;圍設定為-49~50
//memo2是輸出後的陣列 procedure TForm1.Button7Click(Sender: TObject);
var
a,b : array of integer;
count : array of integer;
i, j, total, len : integer;
begin len:=memo3.Lines.Count;// get the size of array
total := 0;
setlength(a,len); //set size
setlength(b,len+1); //set size
setlength(count,100); //set size for i:=0 to len-1 do
begin
a[i]:=strtoint(memo3.Lines[i]);
end; for i:=0 to len-1 do
begin
count[a[i]]:=0;
end; for i:=0 to len-1 do //count hw many a[i]
begin
count[a[i]]:=count[a[i]]+1;
end; for i:=0 to len-1 do //count amout of a[j] ) then
begin
count[a[i]]:=count[a[i]] count[a[j]];
end
else if ( a[i]0 ) then // more than one element case
begin
total := 1;
end; for i:=1 to len-1 do
begin
for j:=0 to total-1 do
begin
if ( a[i]=a[j] ) then break;
end;
if ( j>=total ) then // no same number found
begin
a[total] := a[i]; // add in new number
total := total 1;
end;
end; for i:=len downto 1 do //to new array
begin
while ( count[a[i]]>0 )do
begin
b[count[a[i]]]:=a[i];
count[a[i]]:=count[a[i]]-1;
end;
end; for i:=0 to len-1 do
begin
a[i]:=b[i 1];
end; for i:=0 to len-1 do
begin
memo2.Lines.Add( inttostr(a[i]) );
end; end; 何需Coding爭峰
千人指 萬人鋒
敢問Coding頂峰
三尺秋水塵不染
天下無雙
------
何需Coding爭峰 千人指 萬人鋒 敢問Coding頂峰 三尺秋水塵不染 天下無雙 |
inglong
初階會員 發表:28 回覆:27 積分:36 註冊:2004-08-13 發送簡訊給我 |
//解決一些bug
procedure TForm1.Button7Click(Sender: TObject);
var
a,at,b : array of integer;
//a:輸入的陣列 at:種類的陣列 b:輸出的結果
ncount,count : array of integer;
//count:種類的記數 ncount:小於a[i]的累計
i, j, total, len : integer;
//len:長度
begin len:=memo3.Lines.Count;// get the size of array
total := 0; setlength(a,len); //set size
setlength(at,len); //set size count type of a
setlength(b,len); //set size
setlength(count,100); //set size
setlength(ncount,100); //set size for i:=0 to len-1 do
begin
a[i]:=strtoint(memo3.Lines[i]);
at[i]:=strtoint(memo3.Lines[i]);
end; for i:=0 to len-1 do
begin
count[a[i]]:=0;
ncount[a[i]]:=0;
end; for i:=0 to len-1 do //count hw many a[i]
begin
count[a[i]]:=count[a[i]]+1;
ncount[a[i]]:=ncount[a[i]]+1;
end; if ( len>0 ) then // more than one element case
begin
total := 1;
end; for i:=0 to len-1 do
begin
for j:=0 to total-1 do
begin
if ( at[i]=at[j] ) then break;
end;
if ( j>=total ) then // no same number found
begin
at[total] := at[i]; // add in new number
total := total +1;
end;
//showmessage(inttostr(at[i])+','+inttostr(count[at[i]]));
end; for i:=0 to total-1 do //count amount of type
begin
for j:=i+1 to total-1 do
begin
if at[i]>at[j] then ncount[at[i]]:=ncount[at[i]]+count[at[j]];
if at[i]
------
何需Coding爭峰 千人指 萬人鋒 敢問Coding頂峰 三尺秋水塵不染 天下無雙 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |