PTA之多项式求值

时间:2023-03-09 02:47:45
PTA之多项式求值

PTA之多项式求值

时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB

函数接口定义:

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=; i<=n; i++ )
scanf(“%lf”, &a[i]);
printf("%.1f\n", f(n, a, x));
return ;
}
/* 你的代码将被嵌在这里 */

输入样例:

2 1.1

1 2.5 -38.7

输出样例:

-43.1

 double f( int n, double a[], double x )
{
int i=,j=;
double sum=,x_n=;;
for(i=;i<=n;i++)
{
sum += a[i] * x_n;
x_n *= x;
}
return sum;
}

作者:耑新新,发布于  博客园

转载请注明出处,欢迎邮件交流:zhuanxinxin@aliyun.com