Pascal問題

2007-10-26 5:53 pm
我想問點樣可以從一大堆數字度

去搵一個最大既數呢?

我唔想用bubble sort既方法搵...以直接d既方法去搵...

我主要想知個做個方向者...無example都ok...

回答 (3)

2007-10-26 6:28 pm
✔ 最佳答案
唔洗做sorting唔洗用bubble sort ge algorithm....

基本上係用squential search ge方法

首先initialize第一個element做max
然後用for loop 逐個element check下係唔係大過依家個max
係就將element ge value assign 俾個max

sample:

var a:array[1..5]of integerl;
i,max:integer;


begin
a[1]:=2;
a[2]:=6;
a[3]:=8;
a[4]:=1;
a[5]:=5;
max:=a[1];
for i:= 2 to 5 do
if a[i]>max then max:=a[i];
writeln(max);
end.
2007-10-26 7:12 pm
Bubble sort is a sorting algorithm, not searching algorithm.
If you prefer to use simple searching algorithm, such as linear or sequential search for the maximum value, you can refer to the following pseudocode:
Let n be the number of values in an array of bin[]
var i, max of integer
max = 1
For i:=2 to number_of_values
If bin[i] > bin[max] Then max = i
End
The maximum value in the list will be bin[max]
=================================
The above code's advantage is minimize the IO operation to the array.
2007-10-26 6:13 pm
由頭到尾掃一次囉

用個 variable 裝住個 max, 最初 assign 做第一個 number
之後如果個 max 細過第二個 number
甘咪 assign max 做個 number
最後就搵到...


收錄日期: 2021-04-13 14:10:41
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20071026000051KK00636

檢視 Wayback Machine 備份