算法——Horner scheme

題目描述

In numerical analysis, the Horner scheme or Horner algorithm, named after William George Horner, is an algorithm for the efficient evaluation of polynomials in monomial form. Horner’s method describes a manual process by which one may approximate the roots of a polynomial equation. The Horner scheme can also be viewed as a fast algorithm for dividing a polynomial by a linear polynomial with Ruffini’s rule.
Application
The Horner scheme is often used to convert between different positional numeral systems — in which case x is the base of the number system, and the ai coefficients are the digits of the base-x representation of a given number — and can also be used if x is a matrix, in which case the gain in computational efficiency is even greater.
History
Even though the algorithm is named after William George Horner, who described it in 1819, the method was already known to Isaac Newton in 1669, and even earlier to the Chinese mathematician Ch’in Chiu-Shao in the 13th century. TASK: write a program to calculate sum of Polynomial by Horner scheme.

輸入

tow lines. The first line have tow numbers,n and x, n<=20, x<=10 The second line have n+1 numbers, a0,a1…an.

輸出

The sum of Polynomial

樣例輸入

5 2
0 1 2 3 4 5

樣例輸出

258

代碼

#include<iostream>
using namespace std;
int main(){
	int n, x, sum = 0;
	cin>>n>>x;
	int a[20];
	for(int i = 0; i <= n; i++){
		cin>>a[i];
	} 
	for(int i = n; i >= 0; i--){
		sum = sum*x + a[i];
	}
	cout<<sum<<endl;
	return 0;
} 

思路

1.Horner 算法是以英國數學家 William George Horner 命名的一種多項式求值的快速算法,同秦九韶算法:
只需要n次乘法和n次加法。在人工計算時,一次大大簡化了運算過程。
2.
把一個n次多項式
在這裏插入圖片描述
改寫成如下形式:
在這裏插入圖片描述
3.求多項式的值時,首先計算最內層括號內一次多項式的值。
4.對於一個n次多項式,至多做n次乘法和n次加法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章