我覺得我的方法應該可行一些,因為要刪除的數只有三個最大,三個最小,沒有必要把所有的數都進行排序,特別是當資料很多時,將會浪費很多的資源!我寫的序如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
dim Arr
Arr=array(91,39,46,24,14,56,31,32,56,89)
call DeleteMax(UBound(Arr),3)’刪除三個最大的數!
call GetBack()’每個數稱以-1,將最小的數變成最大的數!
call DeleteMax(UBound(Arr),3)’刪除稱以-1以後三個最大的數!
call GetBack()’還原每一個數
for kk= 0 to UBound(Arr)’輸出每一個數
Response.Write(Arr(kk))
Response.Write("<br>")
next
sub DeleteMax(Arr_total,willdelete)
for m= 1 to willdelete
for i= 0 to Arr_total-m
if Arr(i)>Arr(i+1) then
call swap(Arr(i),Arr(i+1))
end if
next
ReDim Preserve Arr(Arr_total-m)
next
end sub
sub swap(num_a,num_b)’交換兩個數
x=num_a
num_a=num_b
num_b=x
end sub
sub GetBack()
for L=0 to UBound(Arr)
Arr(L)=Arr(L)*-1
next
end sub
%>其中有三個子程式:
1、DeleteMax(Arr_total,willdelete)
功能:刪除若干個最大的數,Arr_total:數組的最大可用下標,willdelete:將要刪除的最大數的個數。
2、swap(num_a,num_b)
功能:交換兩個數
3、GetBack()
功能:將數組的每個元素乘以-1,將最小的數變成最大的數!
助人心得:
實驗出了怎麼刪除一個數組元素!
巧妙應用redim和Preserve語句,如:
dim A()
redim A(10)
’想刪除第六個元素A(5)
for n= 5 to ubound(A)-1
A(n)=A(n+1)
next
redim Preserve A(ubound(A)-1)以上的代碼,就能夠完成了,不信你試試...