Horner规则求多项式

/* Horner */
/*多项式:A(x)=a[n]X^n+a[n-1]x^n-1+...+a[1]X^1+a[0]X^0*/
#include <stdio.h>

long int horner(int coefficient[], int n, int x)
/*coefficient[]为待求多项式的系数数组,n为数组大小,x为多项式中未知数x的具体值*/
{
/*注意:coefficient[0]存放系数a0,coefficient[1]存放系数a1,以此类推*/
    int i;
    long int result;

    result = coefficient[n-1];
    for(i = 1; i <= n-1; i++)
    {
        result = result * x + coefficient[n-1-i];
    }

    return result;
}
int main(void)
{
    long int p;
    int a[4] = {3, 2, 1, 1};

    p = horner(a, 4, 1);

    printf("polynomial x^3 + x^2 + 2x + 3 = %ld\n", p);

    return 0;
}

posted on 2017-12-30 10:33  MACHINE_001  阅读(160)  评论(0编辑  收藏  举报

导航