最多几个直角三角形python-代码实现

时间:2024-03-11 22:06:05

def is_vertical(i, j, k):
    if i ** 2 == j ** 2 + k ** 2:
        return True
    return False


def rec(list_1):
    if len(list_1) < 3:
        return 0
    if len(list_1) == 3:
        if is_vertical(list_1[2], list_1[1], list_1[0]):
            return 1
        return 0
    count = 0
    for i in range(len(list_1) - 1, 1, -1):
        flag = False
        for j in range(0, i):
            for k in range(j + 1, i):
                if is_vertical(list_1[i], list_1[j], list_1[k]):
                    count += 1
                    flag = True
                    new_list = list_1[:]
                    new_list.pop(i)
                    new_list.pop(k)
                    new_list.pop(j)
                    break
            if flag:
                break
        if flag:
            break
    count += rec(new_list)
    return count


rows = int(input().strip())
nums = []
for i in range(rows):
    a = list(map(int, input().strip().split(' ')))[1:]
    a.sort()
    nums.append(a)
for num_list in nums:
    print(rec(num_list))

输入
2
7 6 8 10 15 17 24 26
14 8 10 7 24 25 8 15 17 9 12 15 9 40 41

输出
2 (8,15,17;10,24,26)
4 (7 24 25;8 15 17;9 12 15;9 40 41)