编码练习:观察钟摆
Coding Exercise: Watch the Pendulum
In physics, for a pendulum with length L and initial angle A, its horizontal >displacement X(T) at time T is given by the formula
X(T) = L × cos(A × cos(T × √9.8/L)) - L × cos(A)
Write a program which takes two lines of input; the first line is L and the >second line is A. The output should be ten lines, giving the values of X(0), >X(1), X(2), ..., X(9). For example, if the first line of input is 53.1 and the >second line of input is 0.8, then the first line of output is 0.0 and the second >line of output is 53.1*cos(0.8*cos(1*√9.8/53.1)) - 53.1*cos(0.8) ~ 2.6689.
import math
L = float(input())
A = float(input())
for T in range (0,9):
print(L*math.cos(A*math.cos(T*math.sqrt(9.8/L))-L*math.cos(A)))
我写了这个,但我不明白我做错了什么?
输入:
53.1
0.8
我的输出:
3.545012155898153
7.383727226708044
17.92714440725987
31.889478979714276
44.23118522394127
51.212404291669216
53.079364553814806
52.890770379027806
52.999922313121566
预期答案:
0.0
2.6689070487226805
9.021742145820763
14.794542557581206
15.73774678328343
11.124903835610114
4.423693604072537
0.27377375601245213
1.295906539090336
6.863309996333497
你的括号放错地方了,改一下
L*math.cos(A*math.cos(T*math.sqrt(9.8/L))-L*math.cos(A))
到
L*math.cos(A*math.cos(T*math.sqrt(9.8/L)))-L*math.cos(A)
这在我的电脑上给出了正确的输出
编辑:
也将 range(0,9)
更改为 range(10)
In physics, for a pendulum with length L and initial angle A, its horizontal >displacement X(T) at time T is given by the formula X(T) = L × cos(A × cos(T × √9.8/L)) - L × cos(A)
Write a program which takes two lines of input; the first line is L and the >second line is A. The output should be ten lines, giving the values of X(0), >X(1), X(2), ..., X(9). For example, if the first line of input is 53.1 and the >second line of input is 0.8, then the first line of output is 0.0 and the second >line of output is 53.1*cos(0.8*cos(1*√9.8/53.1)) - 53.1*cos(0.8) ~ 2.6689.
import math
L = float(input())
A = float(input())
for T in range (0,9):
print(L*math.cos(A*math.cos(T*math.sqrt(9.8/L))-L*math.cos(A)))
我写了这个,但我不明白我做错了什么?
输入:
53.1
0.8
我的输出:
3.545012155898153
7.383727226708044
17.92714440725987
31.889478979714276
44.23118522394127
51.212404291669216
53.079364553814806
52.890770379027806
52.999922313121566
预期答案:
0.0
2.6689070487226805
9.021742145820763
14.794542557581206
15.73774678328343
11.124903835610114
4.423693604072537
0.27377375601245213
1.295906539090336
6.863309996333497
你的括号放错地方了,改一下
L*math.cos(A*math.cos(T*math.sqrt(9.8/L))-L*math.cos(A))
到
L*math.cos(A*math.cos(T*math.sqrt(9.8/L)))-L*math.cos(A)
这在我的电脑上给出了正确的输出
编辑:
也将 range(0,9)
更改为 range(10)