循环改递归

hanfeng685516 2008-03-10 10:06:32
For Y0 = 0 To b(1)
For Y1 = 0 To b(2)
For Y2 = 0 To b(3)
For Y3 = 0 To b(4)
For Y4 = 0 To b(5)
For Y5 = 0 To b(6)
For Y6 = 0 To b(7)
For Y7 = 0 To b(8)
For Y8 = 0 To b(9)
For Y9 = 0 To b(10)
For Y10 = 0 To b(11)
For Y11 = 0 To b(12)
For Y12 = 0 To b(13)
For Y13 = 0 To b(14)
For Y14 = 0 To b(15)
For Y15 = 0 To b(16)
For Y16 = 0 To b(17)
For Y17 = 0 To b(18)
For Y18 = 0 To b(19)
For Y19 = 0 To b(20)

If Sheet1.Cells(2, 2) - (Y0 * a(1) + Y1 * a(2) + Y2 * a(3) + Y3 * a(4) + Y4 * a(5) + Y5 * a(6) + Y6 * a(7) + Y7 * a(8) + Y8 * a(9) + Y9 * a(10) + Y10 * a(11) _
+ Y11 * a(12) + Y12 * a(13) + Y13 * a(14) + Y14 * a(15) + Y15 * a(16) + Y16 * a(17) + Y17 * a(18) + Y18 * a(19) + Y19 * a(20)) < Sheet1.Cells(2, 3) And _
Sheet1.Cells(2, 2) - (Y0 * a(1) + Y1 * a(2) + Y2 * a(3) + Y3 * a(4) + Y4 * a(5) + Y5 * a(6) + Y6 * a(7) + Y7 * a(8) + Y8 * a(9) + Y9 * a(10) + Y10 * a(11) _
+ Y11 * a(12) + Y12 * a(13) + Y13 * a(14) + Y14 * a(15) + Y15 * a(16) + Y16 * a(17) + Y17 * a(18) + Y18 * a(19) + Y19 * a(20)) >= 0 Then
w = w + 1
Sheet2.Cells(w, 2) = Y0
Sheet2.Cells(w, 3) = Y1
Sheet2.Cells(w, 4) = Y2
Sheet2.Cells(w, 5) = Y3
Sheet2.Cells(w, 6) = Y4
Sheet2.Cells(w, 7) = Y5
Sheet2.Cells(w, 8) = Y6
Sheet2.Cells(w, 9) = Y7
Sheet2.Cells(w, 10) = Y8
Sheet2.Cells(w, 11) = Y9
Sheet2.Cells(w, 12) = Y10
Sheet2.Cells(w, 13) = Y11
Sheet2.Cells(w, 14) = Y12
Sheet2.Cells(w, 15) = Y13
Sheet2.Cells(w, 16) = Y14
Sheet2.Cells(w, 17) = Y15
Sheet2.Cells(w, 18) = Y16
Sheet2.Cells(w, 19) = Y17
Sheet2.Cells(w, 20) = Y18
Sheet2.Cells(w, 21) = Y19
Sheet2.Cells(w, 22) = w - 1
Sheet2.Cells(w, 23) = Sheet1.Cells(2, 2) - (Y0 * a(1) + Y1 * a(2) + Y2 * a(3) + Y3 * a(4) + Y4 * a(5) + Y5 * a(6) + Y6 * a(7) + Y7 * a(8) + Y8 * a(9) + Y9 * a(10) + Y10 * a(11) _
+ Y11 * a(12) + Y12 * a(13) + Y13 * a(14) + Y14 * a(15) + Y15 * a(16) + Y16 * a(17) + Y17 * a(18) + Y18 * a(19) + Y19 * a(20))
Sheet2.Cells(w, 24) = "x" + Str(w - 1)
End If

Next Y19
Next Y18
Next Y17
Next Y16
Next Y15
Next Y14
Next Y13
Next Y12
Next Y11
Next Y10
Next Y9
Next Y8
Next Y7
Next Y6
Next Y5
Next Y4
Next Y3
Next Y2
Next Y1
Next Y0

...全文
189 点赞 收藏 20
写回复
20 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yidie 2008-03-14
Chen8013写成Chen0813,不好意思,发了才知道,又不能修改。
回复
yidie 2008-03-14
借用Chen0813的代码

Dim k&, w&, n&, sab&,temp&,c&(1 To 20)

Public Sub ArrRecursion(p&, j&)

Dim i&

c(p + 1) = j

If (p < 20) Then
For i = 0 To b(p)
Call ArrRecursion(p + 1, i)
Next
Else
sab = 0
For k = 1 To 19
sab = sab + c(k) * a(k)
Next
For k = 0 To b(20)
temp = k * a(20)
If Sheet1.Cells(2, 2) - temp - sab < Sheet1.Cells(2, 3) _
And Sheet1.Cells(2, 2) - temp - sab >= 0 Then
w = w + 1
For n = 1 To 20
Sheet2.Cells(w, n + 1) = c(n)
Next
Sheet2.Cells(w, 22) = w - 1
Sheet2.Cells(w, 23) = Sheet1.Cells(2, 2) - temp - sab
Sheet2.Cells(w, 24) = "x" + Str(w - 1)
End If
Next
End If

End Sub

回复
hanfeng685516 2008-03-14
 不好意思,献丑了……昨天理解错误………………

  把解决方案重新贴一下,应该用这样的代码:

这种方法我知道,可是这只是形式上是对的,可是结果和我的就不同,调用本身和goto语句也没什么两样的
回复
cbm6666 2008-03-14
分析需求, 再好好的规划一下你的 "算法" , 编程30多年也没见过这种写法的.



回复
flyingscv 2008-03-14
很强大!眼晕中!
回复
forbearORfolie 2008-03-13
引宋丹丹话:看别人的代码要钱,看LZ代码,要命呐—
估计windows内核代码的复杂程度也不过如此吧
回复
[Quote=引用 8 楼 hanfeng685516 的回复:]
别只会说就好,把改好的写上去,我可以提醒你们这个没那么简单,要用到数组知识
[/Quote]

建议不要用这种语气~~你是来求人的.别人对着你这眼花的代码提意见,你别不愿意听.

另外,既然"没那么简单",那就多上点分吧:)

PS:

不要针对我啊.....我只是路过.....
回复
舉杯邀明月 2008-03-13
  不好意思,献丑了……昨天理解错误………………

  把解决方案重新贴一下,应该用这样的代码:
Sub ArrRecursion(aa&(), p&)

Static M&
Dim i&

M = UBound(aa)
If (p < M) Then
For i = 0 To aa(p)
Call ArrRecursion(aa, p + 1)
Next
Else
For i = 0 To aa(p)
'在这里写最内层的执行语句
'………………
'………………

Next
End If

End Sub

  调用的时候,参数p要传入0,比如:Call ArrRecursion(aa, 0)

  可以简单的验证一下:

Option Explicit
Dim mlRunTotal&

Sub ArrRecursion(aa&(), p&)

Static M&
Dim i&

M = UBound(aa)
If (p < M) Then
For i = 0 To aa(p)
Call ArrRecursion(aa, p + 1)
Next
Else
For i = 0 To aa(p)
mlRunTotal = mlRunTotal + 1
Next
End If

End Sub

Private Sub cmdTest_Click()

Dim A&(), K&, N&, S&
Dim i%

K = 5

ReDim A(K)
Randomize
S = 1
Debug.Print
For i = 0 To K
N = Rnd() * 8 + 2 '简单的限制一下值域
A(i) = N
S = S * (N + 1)
Debug.Print i; ">>>"; N
Next
Debug.Print "理论执行次数:"; S

mlRunTotal = 0
Call ArrRecursion(A, 0)
Debug.Print "实际执行次数:"; mlRunTotal

End Sub
回复
qiu5208 2008-03-13
[Quote=引用 9 楼 hanfeng685516 的回复:]
晕倒,最多2层循环可以搞定的东西

看来这们人兄,VB等级很差,
[/Quote]

差在那没看出来。
回复
舉杯邀明月 2008-03-12
[Quote=引用 9 楼 hanfeng685516 的回复:]
晕倒,最多2层循环可以搞定的东西

看来这们人兄,VB等级很差,
[/Quote]

  同意!!!!
  20层的嵌套循环,确实是第一次见到!!!!要是你的数组b()有2000个元素,岂不是要写出一个二千层嵌套的循环来?
  再说,这与“递归”扯不上什么关系吧?

  其实用这样的代码就行了:
Dim i%, j%, M%

M = UBound(b)
For i = 0 To M
for j = 0 To b(i)
'这里是你的循环体中的代码
'..........

Next '(Next j)
Next '(Next i)
回复
看不清
回复
hanfeng685516 2008-03-11
晕倒,最多2层循环可以搞定的东西

看来这们人兄,VB等级很差,
回复
hanfeng685516 2008-03-11
别只会说就好,把改好的写上去,我可以提醒你们这个没那么简单,要用到数组知识
回复
楼主发的这个真是低效代码的典范啊
回复
晕倒,最多2层循环可以搞定的东西
回复
qiu5208 2008-03-10
长见识了
回复
wangwei1979 2008-03-10
循环嵌套20次,第一次看到,佩服
回复
lertulo 2008-03-10
呵呵
回复
33184777 2008-03-10
这种代码真垃圾。
回复
daisy8675 2008-03-10
眼花中,膜拜下
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7489

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-03-10 10:06
社区公告
暂无公告