计算递归函数内接收的数据



函数通过print((输出字符" abc"的所有可能组合。(取决于指定的长度(我需要计算这个数量。我只设法通过打印((一个一个一个一个组来输出这些组合。我在代码的右侧留下了评论。

func allLexicographicRecur (_ string: [String.Element], _ data: [String], _ last: Int, _ index: Int){
    var length = string.count-1
    var data = data
    for i in 0...length {
        data[index] = String(string[i])
        if index == last {
            print(data.joined()) // Displays a combination. It is necessary to somehow calculate.
        }else{
            allLexicographicRecur(string, data, last, index+1)
        }
    }
}

func allLexicographic(_ l: Int) {
    var alphabet = "abc"
    var data = Array(repeating: "", count: l)
    var string = alphabet.sorted()
    var counter = 0
    allLexicographicRecur(string, data, l-1, 0)
}

allLexicographic(3)

该功能必须以某种方式返回这些组合的数量。

我非常感谢您的帮助!

我设法仅以这种方式计数(但很可能不是最好的方法(:

var count = 0
func allLexicographicRecur (_ string: [String.Element], _ data: [String], _ last: Int, _ index: Int){
    var length = string.count-1
    var data = data
    for i in 0...length {
        data[index] = String(string[i])
        if index == last {
            print(data.joined()) // Displays a combination. It is necessary to somehow calculate.
            count += 1
        }else{
            allLexicographicRecur(string, data, last, index+1)
        }
    }
}

func allLexicographic(_ l: Int) {
    var alphabet = "abc"
    var data = Array(repeating: "", count: l)
    var string = alphabet.sorted()
    var counter = 0
    allLexicographicRecur(string, data, l-1, 0)
}

allLexicographic(3)
print(count)

您不需要全局变量。至少还有两个选择。您可以将inout参数添加到allLexicographicRecur以跟踪计数,也可以让allLexicographicRecur返回其计数。

这是您使用返回值的代码:

func allLexicographicRecur(_ string: [String.Element], _ data: [String], _ last: Int, _ index: Int) -> Int {
    let length = string.count - 1
    var data = data
    var count = 0
    for i in 0...length {
        data[index] = String(string[i])
        if index == last {
            print(data.joined()) // Displays a combination. It is necessary to somehow calculate.
            count += 1
        } else {
            count += allLexicographicRecur(string, data, last, index + 1)
        }
    }
    return count
}
func allLexicographic(_ l: Int) -> Int {
    let alphabet = "abc"
    let data = Array(repeating: "", count: l)
    let string = alphabet.sorted()
    return allLexicographicRecur(string, data, l - 1, 0)
}
print(allLexicographic(3))

以下是您更新的代码以使用inout参数。

func allLexicographicRecur(_ string: [String.Element], _ data: [String], _ last: Int, _ index: Int, _ count: inout Int){
    let length = string.count - 1
    var data = data
    for i in 0...length {
        data[index] = String(string[i])
        if index == last {
            print(data.joined()) // Displays a combination. It is necessary to somehow calculate.
            count += 1
        } else {
            allLexicographicRecur(string, data, last, index + 1, &count)
        }
    }
}
func allLexicographic(_ l: Int) -> Int {
    let alphabet = "abc"
    let data = Array(repeating: "", count: l)
    let string = alphabet.sorted()
    var counter = 0
    allLexicographicRecur(string, data, l - 1, 0, &counter)
    return counter
}
print(allLexicographic(3))

由于递归功能,您无法在没有全局变量的情况下管理计数。因此,您所编写的方法是根据您想要的输出的完美方法。

最新更新