计算N个数的和等于给定数

erander 2008-02-12 11:14:29
需设计一个asp程序,是关于在一组不规则的正数中,例如1,2,34,5,6,7,83,可能数量有40个左右,查找任意组合的和是定数例如是100的组合,用递归法死机,求那位大侠帮助一下,最好用asp写,
...全文
98 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
erander 2008-05-11
  • 打赏
  • 举报
回复
谢谢了
Dogfish 2008-02-15
  • 打赏
  • 举报
回复

<%
str = "2,5,20,40,60,38,33,67,22,31,54,32,6,4,9,13,45,6"
str = sortstr(str, ",")
answer = 100
temp = t("",Split(str, ","), answer)
temp_a = Split(temp, "*")

temp1 = "*"
For ii=0 To UBound(temp_a)
'temp2 = sortstr(temp_a(ii), "+")
temp2 = temp_a(ii)
If InStr(temp1, "*" & temp2 & "*")=0 Then
temp1 = temp1 & temp2 & "*"
End If
Next
temp1 = Right(temp1, Len(temp1)-1)
response.write Replace(temp1, "*","<br>")

Function t(a, b, c)
r = ""
a1=a

For i=0 To UBound(b)
b1_str = ""
For j=i+1 To UBound(b)
If b1_str = "" Then
b1_str = b(j)
Else
b1_str = b1_str & "," & b(j)
End If
Next
b1 = Split(b1_str, ",")

If a = "" Then
a1 = b(i)
Else
a1 = a & "+" & b(i)
End If
If eval(a1) < c And UBound(b1)>0 Then
r1 = t(a1, b1, c)
If r1 <> "" Then
If r = "" Then
r = r1
Else
r = r & "*" & r1
End If
End If
ElseIf eval(a1) = c Then
If r = "" Then
r = a1
Else
r = r & "*" & a1
End If
End If
Next
t = r
End Function

Function sortstr(pstr, psign)
pstr_a = Split(pstr, psign)
For i = 0 To UBound(pstr_a)
For j = i To UBound(pstr_a)
If CInt(pstr_a(i))>CInt(pstr_a(j)) Then
k = pstr_a(i)
pstr_a(i) = pstr_a(j)
pstr_a(j) = k
End If
Next
Next
sortstr = ""
For i = 0 To UBound(pstr_a)
If sortstr = "" Then
sortstr = pstr_a(i)
Else
sortstr = sortstr & psign & pstr_a(i)
End If
Next
End Function
%>

28,390

社区成员

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

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