红温了,怎么最后一个测试点RE,python

P5735 【深基7.例1】距离函数

westblot @ 2024-11-27 12:42:32

l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l3=list(map(int,input().split()))
x1=l1[0];y1=l1[1]
x2=l2[0];y2=l2[1]
x3=l3[0];y3=l3[1]
a=(x1-x2)**2+(y1-y2)**2
b=(x1-x3)**2+(y1-y3)**2
c=(x2-x3)**2+(y2-y3)**2
a=a**0.5
b=b**0.5
c=c**0.5
n=a+b+c
print("%.2f"%n)

by 0oOo0 @ 2024-11-27 12:51:46

可能是因为浮点数不精确,用 decimal 就对了:

from decimal import Decimal  
l1 = list(map(Decimal, input().split()))  
l2 = list(map(Decimal, input().split()))  
l3 = list(map(Decimal, input().split()))  
x1, y1 = l1  
x2, y2 = l2  
x3, y3 = l3  
a = ((x1 - x2) ** 2 + (y1 - y2) ** 2).sqrt()  
b = ((x1 - x3) ** 2 + (y1 - y3) ** 2).sqrt()  
c = ((x2 - x3) ** 2 + (y2 - y3) ** 2).sqrt()  
n = a + b + c  
print(f"{n:.2f}")

|