← 返回题库
初级

妖梦拼木棒

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve(lengths):
    from collections import Counter
    lengths = list(map(int, lengths.split(',')))
    count = Counter(lengths)
    total = 0
    for l in count:
        if count[l] >= 2:
            pairs = count[l] * (count[l] - 1) // 2
            for other in count:
                if other < l:
                    total += pairs * count[other]
    print(total)

示例

输入
solve('1,2,3,4,5,6,7,8,9')
期望输出
0
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码