日期:2026年5月19日
ZeroJudge 題目連結:d269. 11579 - Triangle Trouble
解題想法
讀取一行測資,將 $n$ 個邊長由大到小排序,再依序讀取連續 3 個邊長 $a, b, c$,如果最長的邊 $a$ 大於等於較短的兩個邊 $b, c$ 加總,無法組成三角形;反之,用海龍公式計算三角形面積。
Python 程式碼
使用時間約為 67 ms,記憶體約為 14.4 MB,通過測試。
def solve():
import sys
result = []
data = sys.stdin.read().split()
T = int(data[0]) # T 組測資
ptr = 1
while ptr < len(data): # 執行 T 次
n = int(data[ptr]) # 首項是邊的數量
ptr += 1
edges = sorted(map(float, data[ptr:ptr+n]), reverse=True) # 邊長,由大到小排序
ptr += n
imax = 0.0 # 最大面積
for i in range(n-2): # 依序讀取邊長,每次 3 項
a, b, c = edges[i:i+3] # 邊長
if a >= b + c: # 最長的邊大於等於另外兩個邊相加
continue # 無法組成三角形,找下一組
s = (a + b + c) * 0.5 # 用海龍公式求三角形面積
area = (s * (s-a) * (s-b) * (s-c))**0.5
if area > imax: imax = area # 更新最大值
result.append(f"{imax:.2f}\n")
sys.stdout.write("".join(result))
if __name__ == "__main__":
solve()