S表示1,L表示2,计算由S和L组成的序列之和为N的组合

def func(n):
    def calc_str(s):
        s = s.strip() if s is not None else ""
        s = s.upper()
        result = 0
        for c in s:
            result += 1 if c == "S" else 2
        return result
    result = list()
    if n == 0:
        result.append("")
    elif n == 1:
        result.append("S")
    else:
        offset = -1
        while (n + offset) >= 0:
            for s in func(n + offset):
                if calc_str(s + "S") == n:
                    result.append(s + "S")
                if calc_str(s + "L") == n:
                    result.append(s + "L")
            offset -= 1
    return result
    
print(func(6))

好像有什么不对的地方,明天再看看

原文地址:https://www.cnblogs.com/amtoor/p/4960477.html