
from itertools import combinations
import math
# print(list(permutations([1, 2, 3, 4], 3))) # 有序排列
# print(list(combinations([1, 2, 3, 4], 3))) # 无序排列
class Triangle(object):
alist = None
def __init__(self, alist):
self.alist = alist
def largestTriangle(self):
res = []
self.alist.sort()
# 对边长升序排序进行无序组合,因此最后一组能构成三角形的值,周长一定是最大的
for val in combinations(self.alist, 3):
# class tuple转list
l = [x for x in val]
# 判断是否能构成三角形
if (l[0] + l[1] > l[2] and l[0] + l[2] > l[1] and l[1] + l[2] > l[0]
and math.fabs(l[0] - l[1]) < l[2] and math.fabs(l[0] - l[2]) < l[1] and math.fabs(l[1] - l[2]) < l[
0]):
res.append([sum(l), l])
return res
# list = [6, 7, 10, 20, 32, 1]
# list = [1, 4, 7]
list = list(map(int, input("输入列表:").split()))
val = Triangle(list)
res = val.largestTriangle()
if (len(res) == 0):
print("所有组合都不能构成有效三角形")
else:
print("最大周长:%d,三边长度分别为:%d、%d、%d" % (res[-1][0], res[-1][1][0], res[-1][1][1], res[-1][1][2]))