6-2 多項(xiàng)式求值(15 分)

問題描述.png
函數(shù)接口定義:
double f( int n, double a[], double x );
其中n是多項(xiàng)式的階數(shù),a[]中存儲(chǔ)系數(shù),x是給定點(diǎn)。函數(shù)須返回多項(xiàng)式f(x)的值。
裁判測(cè)試程序樣例:
#include <stdio.h>
#define MAXN 10
double f( int n, double a[], double x );
int main()
{
int n, i;
double a[MAXN], x;
scanf("%d %lf", &n, &x);
for ( i=0; i<=n; i++ )
scanf(“%lf”, &a[i]);
printf("%.1f\n", f(n, a, x));
return 0;
}
/* 你的代碼將被嵌在這里 */
double f( int n, double a[], double x )
{
double fx = 0.0;
double temp = 1.0;
for(int i=0; i<=n; i++) {
if(i != 0) {
temp *= x;
}
fx += a[i] * temp;
}
return fx;
}
輸入樣例:
2 1.1
1 2.5 -38.7
輸出樣例:
-43.1