您将得到截面直径和线段或弦的长度。我问题的直径是12,和弦是10。您必须找到阴影部分的高度,然后打印该区域。原始公式是A=2/3ch + h^3/2c
。我的同学在这个区域得到了18分,但是当我使用我的代码时,我得到了41分。在
这是我能找到的最接近的图片。但是有一条虚线从ϴ
到s
.
from math import sqrt
diamStr=input("Enter the length of the diameter: ")
diameter=int(diamStr)
chordStr = input( " Enter the chord length: ")
chord = int(chordStr)
radius = (diameter/2)
s = sqrt (diameter**2+chord**2)
h = (s/2-radius)
i= (2/3*chord*h)
j=(h**3/2*chord)
area = (i+j)
print (area)
不幸的是,你的公式出了点问题,但是如果你看看一些初等数学的问题,你可能会注意到,角
ϴ
可以用cosine rule找到,因为我们知道3个长度(两个半径和弦长)在Python中是:
由于变量
theta
已经以弧度表示,我们可以使用以下公式计算线段的面积:在python中是
area = 1/2 * (theta - math.sin(theta)) * radius**2
因此,在合并所有这些之后,我们得出了一个优雅的解决方案:
^{pr2}$如果输入的直径为12cm,弦长为10,您将得到
18.880864248381847
,但是您可以通过round()
函数将其四舍五入到任何小数位数。在例如:
print(round((area),2))
打印18.88
相关问题 更多 >
编程相关推荐