题目描述:给定一个数组,都是整数,比如[2,2,3,4],判断数组中的数字能组成多少种三角形?
#给定一个数组,判断组成三角形的有多少种组合
def help(nums):
a,b,c = nums[0],nums[1],nums[2]
if a+b>c and a+c>b and c+b>a and a-b<c and a-c<b and c-b<a:
return True
return False
def huisu(index,path,count,nums):
if len(path)==3 and help(path):
count.append(0)
return
if len(path)>3:
return
for i in range(index,len(nums)):
path.append(nums[i])
huisu(i+1,path,count,nums)
path.pop()
count = []
nums = [2,2,3,4]
huisu(0,[],count,nums)
print(len(count))