在我用 100 個介于 1 和 11 之間的隨機值填充陣列后,如何確定哪個值出現最多?這是一個簡單的問題,但我似乎無法在網上找到任何直接的答案。
uj5u.com熱心網友回復:
這是一個示例代碼:
procedure TForm1.Button1Click(Sender: TObject);
function Calculate: Integer;
var
Numbers: array [1..100] of Byte;
Counts: array [1..11] of Byte;
I: Byte;
begin
// Fill the array with random numbers
for I := Low(Numbers) to High(Numbers) do
Numbers[I] := Random(11) 1;
// Count the occurencies
ZeroMemory(@Counts, SizeOf(Counts));
for I := Low(Numbers) to High(Numbers) do
Inc(Counts[Numbers[I]]);
// Identify the maximum
Result := Low(Counts);
for I := Low(Counts) 1 to High(Counts) do
if Counts[I] > Counts[Result] then
Result := I;
end;
begin
ShowMessage(Calculate.ToString);
end;
uj5u.com熱心網友回復:
這是一個簡單的問題 [...]
是的
但我似乎無法在網上找到任何直接的答案。
您不應該在線搜索解決方案;相反,您應該開始考慮如何設計一種能夠解決問題的演算法。為此,您可能需要筆和紙。
首先,我們需要一些資料來處理:
const
ListLength = 100;
MinValue = 1;
MaxValue = 11;
function MakeRandomList: TArray<Integer>;
begin
SetLength(Result, ListLength);
for var i := 0 to High(Result) do
Result[i] := MinValue Random(MaxValue - MinValue 1);
end;
該MakeRandomList函式創建一個動態整數陣列。根據需要,該陣列包含ListLength = 100范圍從MinValue = 1到的整數MaxValue = 11。
現在,給定這樣一個整數串列,
var L := MakeRandomList;
我們如何找到最頻繁的值?
好吧,如果我們要在沒有計算機的情況下解決這個問題,只使用筆和紙,我們可能會計算每個不同值 (1, 2, ..., 11) 在串列中出現的次數,不是嗎?
然后我們只需要找到頻率最高的值。
例如,給定資料
2, 5, 1, 10, 1, 5, 2, 7, 8, 5
我們會數數以找到頻率
X Freq
2 2
5 3
1 2
10 1
7 1
8 1
然后我們從頂行到底行讀取表格以找到頻率最高的行,不斷跟蹤當前的獲勝者。
現在我們知道如何解決這個問題,撰寫一段代碼來執行這個演算法就很簡單了:
procedure FindMostFrequentValue(const AList: TArray<Integer>);
type
TValueAndFreq = record
Value: Integer;
Freq: Integer;
end;
var
Frequencies: TArray<TValueAndFreq>;
begin
if Length(AList) = 0 then
raise Exception.Create('List is empty.');
SetLength(Frequencies, MaxValue - MinValue 1);
// Step 0: Label the frequency list items
for var i := 0 to High(Frequencies) do
Frequencies[i].Value := i MinValue;
// Step 1: Obtain the frequencies
for var i := 0 to High(AList) do
begin
if not InRange(AList[i], MinValue, MaxValue) then
raise Exception.CreateFmt('Value out of range: %d', [AList[i]]);
Inc(Frequencies[AList[i] - MinValue].Freq);
end;
// Step 2: Find the winner
var Winner: TValueAndFreq;
Winner.Value := 0;
Winner.Freq := 0;
for var i := 0 to High(Frequencies) do
if Frequencies[i].Freq > Winner.Freq then
Winner := Frequencies[i];
ShowMessageFmt('The most frequent value is %d with a count of %d.',
[Winner.Value, Winner.Freq]);
end;
uj5u.com熱心網友回復:
Delphi 有一個TDictionary類,您可以使用它來實作頻率圖,例如:
uses
..., System.Generics.Collections;
function MostFrequent(Arr: array of Integer) : Integer;
var
Frequencies: TDictionary<Integer, Integer>;
I, Freq, MaxFreq: Integer;
Elem: TPair<Integer, Integer>;
begin
Frequencies := TDictionary<Integer, Integer>.Create;
// Fill the dictionary with numbers
for I := Low(Arr) to High(Arr) do begin
if not Frequencies.TryGetValue(Arr[I], Freq) then Freq := 0;
Frequencies.AddOrSetValue(Arr[I], Freq 1);
end;
// Identify the maximum
Result := 0;
MaxFreq := 0;
for Elem in Frequencies do begin
if Elem.Value > MaxFreq then begin
MaxFreq := Elem.Value;
Result := Elem.Key;
end;
end;
Frequencies.Free;
end;
var
Numbers: array [1..100] of Integer;
I: Integer;
begin
// Fill the array with random numbers
for I := Low(Numbers) to High(Numbers) do
Numbers[I] := Random(11) 1;
// Identify the maximum
ShowMessage(IntToStr(MostFrequent(Numbers)));
end;
uj5u.com熱心網友回復:
我還在學習,因此覺得我處理這個問題的方式可能更接近于我的方式:
procedure TForm1.GetMostOccuring;
var
arrNumbers : array[1..100] of Integer;
iNumberWithMost : Integer;
iNewAmount, iMostAmount : Integer;
I, J : Integer;
begin
for I := 1 to 100 do
arrNumbers[I] := Random(10) 1;
iMostAmount := 0;
for I := 1 to 10 do
begin
iNewAmount := 0;
for J := 1 to 100 do
if I = arrNumbers[J] then
inc(iNewAmount);
if iNewAmount > iMostAmount then
begin
iMostAmount := iNewAmount;
iNumberWithMost := I;
end;
end;
ShowMessage(IntToStr(iNumberWithMost));
end;
我希望這不是完全沒用的。這只是對一個簡單問題的簡單回答。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/484337.html
標籤:德尔福
