本題的木棒長度都是整數,所以沒有等腰直角三角形,所有的三角形都是三邊長相異。
根據題目,所有的木棒都是不同的,所以輸入3 3 4 4 5 5
,可以排出邊長為3、4、5的直角三角形,但是木棒都不同,所以可以排出2*2*2=8
種。
所以可以建立一個字典,儲存邊長:數量
的訊息。
input = "3 3 4 4 5 5"
--> wood_count = {3:2, 4:2, 5:2}
。
把所有的木棒,不同的長度,儲存成list,並且升序排列。
-->diff_woods = [3, 4, 5]
。
然後依次檢測,是否有a*a+b*b=c*c的狀況,如果有的話,把答案組合儲存到另一個combination=[]
裡面。
--> combinations = [[3, 4, 5]]
。
最後,把combinations
的元素,裡面的邊長,進去字典裡面,取得該長度的木棒有幾根,再相乘,加總起來。
參考
question_count = int(input()) #輸入題數
for _ in range(question_count):
_ = int(input()) #輸入該題有幾根木棒(用不到)
woods = list(map(int,input().split())) #輸入該題的木棒長度
diff_woods = sorted(list(set(woods)))
wood_count = {}
for i in diff_woods:
wood_count[i] = woods.count(i)
combinations = []
for i in range(len(diff_woods)):
for j in range(i+1,len(diff_woods)):
for k in range(j+1, len(diff_woods)):
if diff_woods[i]*diff_woods[i] + diff_woods[j]*diff_woods[j] == diff_woods[k]*diff_woods[k]:
combinations.append([diff_woods[i],diff_woods[j],diff_woods[k]])
total = 0
for t in combinations:
total += wood_count[t[0]]*wood_count[t[1]]*wood_count[t[2]]
print(total)
|