[发布]组合的算法(vbs)

gu1dai 2005-04-06 08:36:13
一个数组所有元素的可能组合
用的是摘果子的算法。

<%
dim result
stra=split("1,2,3,4,5",",")
call combo(stra)

sub combo(str)
for i=0 to ubound(str)
ftmp=pick(str,i)
combo(ftmp)
next
end sub

function pick(strarray,k)
for i=0 to ubound(strarray)
if i<>k then temp=temp&strarray(i)&","
next
if len(temp)>1 then
temp=left(temp,len(temp)-1)
end if
ww temp
pick=split(temp,",")
end function

sub ww(t)
response.write t
response.write "<BR>"
response.flush
end sub
%>
...全文
117 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gu1dai 2005-04-07
  • 打赏
  • 举报
回复
在我的机子上测试10万次8秒。
ok,可以说是太慢了。

gu1dai 2005-04-07
  • 打赏
  • 举报
回复
补充:上面的算法只是雏形而已:
今天完成了调出特定组合的元素的算法
运行下你就明白了。

<!--本過程完成對數組元素取特定組合-->

<%
dim result,geted
dim limvalue
t5=timer
ysstr=split("1,20,30,4,51",",")
main

ww timer-t5
''------------------main完成初始化,及結果處理--------------
sub main
'----------------------初始化-----------
limvalue=75
strsub=0
for j=1 to ubound(ysstr)
strsub=strsub&","&j
next
strsub=split(strsub,",")
'-----------------------開始組合---------------
call combo(strsub)
'------------------------result是全局變量,存放下標--------------
xb=split(result,",")
'------------------------輸出結果-----------------------
for i=0 to ubound(xb)
xbs=cint(xb(i))
'ww ysstr(xbs)
next
end sub
''-----------------------main結束-------------------------

''---------------------主過程 combo-----------------------
sub combo(str)
for i=0 to ubound(str)
if geted then
exit sub
end if
ftmp=pick(str,i)
combo(ftmp)
next
end sub
''----------------------子過程pick---------------------------
function pick(strarray,k)
for i=0 to ubound(strarray)
if i<>k then
temp=temp&strarray(i)&","
xb=cint(strarray(i))
numtmp=numtmp+cint(ysstr(xb))
end if
next
if numtmp=limvalue then '如果結果等於限制的結果
ww numtmp
geted=true
result=left(temp,len(temp)-1)
end if
if len(temp)>1 then
temp=left(temp,len(temp)-1)
end if

pick=split(temp,",")
end function

sub ww(t)
response.write t
response.write "<BR>"
response.flush
end sub
%>

meizz 2005-04-06
  • 打赏
  • 举报
回复
这个算法问题大狼曾经开过一个帖子讨论, 算法比你的精典, 你可以去看看
gu1dai 2005-04-06
  • 打赏
  • 举报
回复
摘果子,一个个地摘下来也。
poonzsp 2005-04-06
  • 打赏
  • 举报
回复
...........
gu1dai 2005-04-06
  • 打赏
  • 举报
回复
我感觉我的算法很好(窃笑),主要是就一个调用,特简单。
超级大笨狼的帖子。
24点算法
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=189390
太复杂了,看的头晕。
  • 打赏
  • 举报
回复
这个都是固化算法,想明白一次就ok了
gu1dai 2005-04-06
  • 打赏
  • 举报
回复
真的阿,我怎么不知道阿。害我想了一晚上。

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧