• 主页

# 求一个比较高效的数组查找算法

llpoo 2009-08-06 04:01:16

a(0)=1
a(1)=1
a(2)=2
a(3)=3
a(4)=1
a(5)=2
a(6)=3
a(7)=4
a(8)=5
a(9)=6

============================

1, 3
2，2
3，2
4，1
5，1
6，1
...全文
138 点赞 收藏 10

10 条回复

xzx99 2009-08-06
a是一定范围内的自然数：
``````
<%
DIM a(9) , b(9,1)
a(0)=1
a(1)=1
a(2)=2
a(3)=3
a(4)=1
a(5)=2
a(6)=3
a(7)=4
a(8)=5
a(9)=6

For i=0 To 9
b(i,0)=i
for j=0 to 9
if   b(i,0)=a(j) then b(i,1)= b(i,1)+1
next
next

For i=0 To 9
if b(i,0)>0 and b(i,1)>0 then Response.Write  b(i,0) & "," & b(i,1) & "<BR>"
next

%>
``````

lzp4881 2009-08-06
<%
dim a : a = Array(1,1,2,3,1,2,3,4,5,6,1,1,4,5,6,2,22,22,222,2,3,4,5,6,6,5,1,2,3,4,5,6,1,2,3,4,5,6,3,2,1,22,222,22,22,222,3,4,5,6,2,3,1,2,3,4,5,6,6,5,4,3,2,1)
str=","&join(a,",")&","
while len(str)<>1
temp=left(str,instr(2,str,","))
l1=len(str)
while instr(str,temp)<>0
str=replace(str,temp,",")
wend
l2=len(str)
response.write replace(temp,",","") &":"& (l1-l2)/(len(temp)-1)&"<br>"
wend
%>

lzp4881 2009-08-06
<%
dim a : a = Array(1,1,2,3,1,2,3,4,5,6,1,1,4,5,6,2,22,22,222,2,3,4,5,6,6,5,1,2,3,4,5,6,1,2,3,4,5,6,3,2,1,22,222,22,22,222,3,4,5,6,2,3,1,2,3,4,5,6,6,5,4,3,2,1)
str=","&join(b,",")&","
while len(str)<>1
temp=left(str,instr(2,str,","))
l1=len(str)
while instr(str,temp)<>0
str=replace(str,temp,",")
wend
l2=len(str)
response.write replace(temp,",","") &":"& (l1-l2)/(len(temp)-1)&"<br>"
wend
%>

llpoo 2009-08-06

toury 2009-08-06
[Quote=引用 5 楼 llpoo 的回复:]
toury,谢谢你的关注。我要的只是得出数组各个值出现的次数。希望算法不要跟语言相关，即ASP里写了这个算法，那么用到php或JS或java或C只要改变几个关键的控制符就好了，不要特定的内置函数
[/Quote]

For Each x In c
response.write x & " = " & c.Item(x)'这里输出的就是“数组各个值出现的次数。”
Next

llpoo 2009-08-06
toury,谢谢你的关注。我要的只是得出数组各个值出现的次数。希望算法不要跟语言相关，即ASP里写了这个算法，那么用到php或JS或java或C只要改变几个关键的控制符就好了，不要特定的内置函数

toury 2009-08-06
``````
<%
a = Array(1, 1, 2, 3, 1, 2, 3, 4, 5, 6)
Set c = CreateObject("Scripting.Dictionary")
a1 = 0: a2 = 0

For i = 0 To UBound(a)
If c.Exists(a(i)) Then
d = CInt(c.Item(a(i)))
d = d + 1
c.Item(a(i)) = CStr(d)
Else
d = 1
End If

If d > a2 Then
a1 = a(i):      a2 = d
End If
Next

For Each x In c
response.write x & " = " & c.Item(x)
Next
response.write "最多的是:"; a1 & ",出现了" & a2 & "次"

%>
``````

llpoo 2009-08-06

shenzhenNBA 2009-08-06

hookee 2009-08-06
``````
DIM a(9)
a(0)=1
a(1)=1
a(2)=2
a(3)=3
a(4)=1
a(5)=2
a(6)=3
a(7)=4
a(8)=5
a(9)=6

rs.CursorLocation = 3
rs.Fields.Append "val", 3
rs.Fields.Append "cnt", 3
rs.Open
rs("val") = a(0)
rs("cnt") = 1
rs.Update
For i=1 To UBound(a)
rs.MoveFirst
rs.Find "val=" & a(i)
If Not rs.EOF Then
rs("cnt") = rs("cnt") + 1
Else
rs("val") = a(i)
rs("cnt") = 1
End If
rs.Update
Next

rs.Sort = "cnt desc"
rs.MoveFirst
i=1
s = ""
Do While Not rs.EOF
s = s & i & ". " & rs("val") & " " & rs("cnt") & VbCrLf & "<br>"
rs.MoveNext
i = i + 1
Loop
Response.Write s
``````

ASP

2.8w+

ASP即Active Server Pages，是Microsoft公司开发的服务器端脚本环境。