您好,登錄后才能下訂單哦!
--We can represent a polynomial anxn + an-1xn-1 + : : : + a1x1 + a0
--in Lua as a list of its coefficients, such as {a0,a1,...,an}.
--Write a function that receives a polynomial (represented as a table) and a
--value for x and returns the polynomial value.
function computePoly(poly,x)
local n = #poly --多項式系數個數
local sum = poly[n] --初始化為多項式最高項數對應系數
for i=n-1,1,-1 do
sum = sum * x + poly[i] --循環n-1次
end
return sum
end
print(computePoly({1,2,4,8,16},2))--第1個參數為多項式對應系數,第二個參數為對應x的值
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。