题目链接:https://pintia.cn/problem-sets/14/problems/734
本题要求实现一个函数,计算阶数为n,系数为a[0] … a[n]的多项式f(x)=∑i=0n(a[i]×xi) 在x点的值。
函数接口定义:
double f( int n, double a[], double x );
其中n是多项式的阶数,a[]中存储系数,x是给定点。函数须返回多项式f(x)的值。
裁判测试程序样例:
#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;
}
/* 你的代码将被嵌在这里 */
输入样例:
2 1.1
1 2.5 -38.7
输出样例:
-43.1
double f( int n, double a[], double x ){
double total = 1;
double sum = 0;
for(int i = 0; i <= n; i++){
sum += a[i] * total;
total *= x;
}
return sum;
}