需要递归语句的帮助 [英] need help with recursive statements

查看:114
本文介绍了需要递归语句的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点痴迷于编码递归函数!

我需要解决的问题是:

在一个人可以得分的飞镖游戏中:7,15 ,每个镜头19,23,29或37分

找到一个组合,其中一名球员得分100分,正好6枪。

所以我这样写:(vb .net)



I kinda suck at coding recursive functions !
the problem I need to solve is this :
in a dart game a person can score :7,15,19,23,29 or 37 points in each shot
find the combinations in which a player scores 100 points with exactly six shots.
so I wrote this : (vb.net )

arr(0) = 7
     arr(1) = 15
     arr(2) = 19
     arr(3) = 23
     arr(4) = 29
     arr(5) = 37

     For i = 0 To 5

         For j = 0 To 5
             For k = 0 To 5

                 For l = 0 To 5

                     For m = 0 To 5


                         For n = 0 To 5

                             sum = arr(i) + arr(j) + arr(k) + arr(l) + arr(m) + arr(n)
                             If sum = 100 Then
                                 My.Computer.FileSystem.WriteAllText("C:\dar.txt", arr(i) & "   " & arr(j) & "   " & arr(k) & "   " & arr(l) & "   " & arr(m) & "   " & arr(n) & vbNewLine, True)
                             End If
                         Next
                     Next

                 Next


             Next


         Next

     Next





但是你可以看到它是一个非常差的解决方案!

我怎么能用递归来做这个?

我首先尝试使用递归,但它只计算第一个组合(7 7 7 7 7 7)然后它会永远(15 7 7 7 7 7)



非常感谢!!



but it is a very poor solution as you can see !
how can I do this using recursion ?
I first tried to use recursion but it computer only the first combination (7 7 7 7 7 7 ) and then it would do (15 7 7 7 7 7 ) forever

thank you very much !!

推荐答案

这不是递归,那是迭代。

要递归地执行此操作,需要一个接受可能镜头和目标值列表的方法。

然后该方法从列表中获取一个镜头,并通过调用自身将剩余部分和新目标传递给自身 - 它用它来计算其余的组合。



我不打算详细介绍 - 对我来说这将是很多工作(和坦率地说,我现在不能为此而烦恼,加上它不会帮助你获得解决方案)但是看一个更简单的例子,它可能有意义:

That isn't recursion, that's iteration.
To do this recursively, you need a method which accepts a list of possible shots and a target value.
The method then takes one of the shots from the list, and passes the remainder and the new target to itself, by calling itself - it uses itself to work out the rest of the combinations.

I'm not going to go into full details - that would be a lot of work for me (and frankly I can't be bothered to do that at the moment, plus it wouldn;t help you to be given teh solution) but look at a simpler example, and it might make sense:
Private Function factorial(value As Integer) As Integer
    If value <= 1 Then
        Return 1
    End If
    Return value * factorial(value - 1)
End Function

5!是5 * 4 !,这是5 *(4 * 3!),即5 *(4 *(3 * 2!))等等,所以它自称为其他迭代。



当你理解它是如何工作的,那么就开始考虑你的问题了 - 它更复杂,但是当你对它进行解决时却不是很多。

5! is 5 * 4!, which is 5 * (4 * 3!) which is 5 * (4 * (3 * 2!)) and so forth, so it calls itself to work out the other "iterations".

When you understand how that works, then start looking at your problem - it's more complicated, but not a lot when you get your head round it.

OriginalGriff给出了一个很好的递归描述,但这个问题实际上更类似于8-queens问题,可以使用回溯 [ ^ ]

这是解决8个女王问题 [ ^ ]

一旦你理解了这一点,你的问题应该很容易编码。
OriginalGriff gives a good description of recursion, but this problem is actually much more similar to the 8-queens problem and can be solved recursively using Backtracking[^]
Here is a link to a description of the recursive backtracking for Solving 8 queen problem[^]
Once you understand this, your problem should be pretty easy to code.


这篇关于需要递归语句的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆