有哪位大神會用python語言撰寫這段程式呀?求救
程式小白第一次接觸編程,把所有的分數都給了
uj5u.com熱心網友回復:
from sympy import *
def φ(x, h):
return (sin(x + h) - sin(x - h)) / 2 / h
def D(n, m):
if m == 1:
return float(φ(pi / 3, 1 / 2 ** n))
return float(4 ** m / (4 ** m - 1) * D(n, m - 1) - 1 / (4 ** m - 1) * D(n - 1, m - 1))
while 1:
p = int(input('請選擇:1(φ),2(D):\n'))
if p == 1:
try:
a = int(input('請輸入行數:'))
for i in range(a):
for j in range(0, i + 1):
print(D(i + 1, j + 1), end='\t')
print()
except Exception:
print('行數為正整數')
continue
elif p == 2:
n = int(input('請輸入適當的n:'))
h = int(input('請輸入適當的h:'))
d = φ(pi / 3, h / 2 ** n)
print(float(d))
else:
print('輸入有誤,請重新輸入\n')
continue
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/139524.html
