霍纳规则用来简化朴素多项式的求值。它将一元n次多项式的求值问题转化为n个一次式。
霍纳规则是采用最少的乘法运算策略,求多项式A(x) = anxn+ an-1xn-1+...+ a1x + a0在x0处的值,该规则是A(x0)=(...((anx0+ an-1)x0+...+ a1)x0+ a0)
1 #include<stdio.h> 2 3 int horner(int a[], int x, int n); 4 5 int main() 6 { 7 int n; 8 scanf("%d", &n); 9 10 int a[n + 1]; 11 int x; 12 for(int i = 0; i <= n; i++){ 13 scanf("%d", &a[i]); 14 } 15 16 scanf("%d", &x); 17 printf("%d", horner(a, x, n)); 18 19 return 0; 20 } 21 22 int horner(int a[], int x, int n) 23 { 24 int i = n; 25 int result = x; 26 while(i > 0){ 27 result = a[i] + result * x; 28 i--; 29 } 30 31 return result; 32 }