内部有循环的递归函数

Posted

技术标签:

【中文标题】内部有循环的递归函数【英文标题】:Recursive function with loops inside 【发布时间】:2021-10-21 10:13:34 【问题描述】:

我正在尝试在递归函数中概括循环...

基本的双循环是以下函数:

func multiLoops(start ix:Int, upTo n:Int) 
    for i in ix...n 
        for j in i+1 ... n+1 
            print("it is \(i) \(j)")
        
    

multiLoops(start: 3, upTo: 6)

结果如下:

it is 3 4
it is 3 5
it is 3 6
it is 3 7
it is 4 5
it is 4 6
it is 4 7
it is 5 6
it is 5 7
it is 6 7

我对递归函数的尝试如下:

func multiLoopsRecursive(start ix:Int, upTo n:Int, loopNumber:Int)
    var loopNumber = loopNumber
    var previous_i = ix
    
    func loop(start ix:Int, upTo n:Int) 
        for i in ix + 1...n 
            print("it is \(previous_i) \(i)")
            if loopNumber > 1 
                loopNumber -= 1
                previous_i = i
            loop(start: previous_i+1, upTo: n+loopNumber)
            
        
    
    loop(start: ix, upTo: n)

multiLoopsRecursive(start: 3, upTo: 6, loopNumber: 2)

结果如下...

it is 3 4
it is 4 6
it is 4 7
it is 4 5
it is 4 6

与基本的双循环不太一样... 我真的被屏蔽了…… 谢谢您的帮助... 问候 克

【问题讨论】:

【参考方案1】:

添加了打印 loopNumber 的基本双循环:

    for i in ix...n 
        print("it is loop 1")
        for j in i+1 ... n+1 
            print("it is loop 2")
            print("it is \(i) \(j)")
        
    

multiLoops(start: 3, upTo: 6)

结果:

it is loop 1
it is loop 2
it is 3 4
it is loop 2
it is 3 5
it is loop 2
it is 3 6
it is loop 2
it is 3 7
it is loop 1
it is loop 2
it is 4 5
it is loop 2
it is 4 6
it is loop 2
it is 4 7
it is loop 1
it is loop 2
it is 5 6
it is loop 2
it is 5 7
it is loop 1
it is loop 2
it is 6 7

Larme,请找到添加了 loopNumber 的代码:


var loopNumber = 1
func multiLoopsRecursive(start: Int, upTo: Int) 
    print("it is loop \(loopNumber)")
    for i in start+1...upTo+1 
        print("it is \(start) \(i)")
    
    let newStart = start + 1
    if newStart < upTo+1 
        loopNumber += 1
        multiLoopsRecursive(start: newStart, upTo: upTo)
    


print("Recursive")
multiLoopsRecursive(start: 3, upTo: 6)

结果:


Recursive
it is loop 1
it is 3 4
it is 3 5
it is 3 6
it is 3 7
it is loop 2
it is 4 5
it is 4 6
it is 4 7
it is loop 3
it is 5 6
it is 5 7
it is loop 4
it is 6 7

感谢您的支持 克

【讨论】:

【参考方案2】:

我们来了! 诀窍是要知道在哪个循环中调整不同的变量。为了更清楚起见,我在代码中保留了“向下和向上打印循环”,但它当然可以被抑制。 现在它适用于循环中的循环:-)

func loopInLoop (iStart:Int, iEnd:Int, numberOfLoops : Int) 
    print("what loop is it! \(loopIndex)")
    var iRank = 0
    
    for i in iStart...(iEnd - numberOfLoops + loopIndex) 
        iArray[loopIndex-1] = i 
        iRank += 1
        if loopIndex < numberOfLoops 
            print ("loop down")
            loopIndex += 1
            loopInLoop(iStart: iStart + iRank, iEnd: iEnd, numberOfLoops: numberOfLoops)
            
         else print("iArray:\(iArray)")
    
    print ("loop up")
    loopIndex -= 1


let iStart = 3 // initial starting index of first loop, must be <= iEnd
let iEnd = 7 // ending starting index of first loop
let numberOfLoops = 2 // number of loop to execute, must be <= (iEnd-iStart)

var iArray = [Int](repeating: 0, count: numberOfLoops) // Array of indexes
var loopIndex = 1 // initial index of the loop

loopInLoop(iStart: iStart, iEnd: iEnd, numberOfLoops : numberOfLoops)

结果:

what loop is it! 1
loop down
what loop is it! 2
iArray:[3, 4]
iArray:[3, 5]
iArray:[3, 6]
iArray:[3, 7]
loop up
loop down
what loop is it! 2
iArray:[4, 5]
iArray:[4, 6]
iArray:[4, 7]
loop up
loop down
what loop is it! 2
iArray:[5, 6]
iArray:[5, 7]
loop up
loop down
what loop is it! 2
iArray:[6, 7]
loop up
loop up

【讨论】:

以上是关于内部有循环的递归函数的主要内容,如果未能解决你的问题,请参考以下文章

在递归函数中使用while循环和if语句之间有区别吗?

Python的递归函数

python 函数的递归操作

递归 C# 函数从 for 循环内部返回 - 如何转换为 F#?

函数7—递归

python递归函数及二分法查找