PTA之多项式求值

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

函数接口定义:

double f( int n, double a[], double x );

其中n是多项式的阶数,a[]中存储系数,x是给定点。函数须返回多项式f(x)的值。

裁判测试程序样例:

 1 #include <stdio.h>
 2 #define MAXN 10
 3 double f( int n, double a[], double x );
 4 int main()
 5 {
 6     int n, i;
 7     double a[MAXN], x;
 8     scanf("%d %lf", &n, &x);
 9     for ( i=0; i<=n; i++ )
10         scanf(“%lf”, &a[i]);
11     printf("%.1f\n", f(n, a, x));
12     return 0;
13 }
14 /* 你的代码将被嵌在这里 */

输入样例:

2 1.1

1 2.5 -38.7

输出样例:

-43.1

 1 double f( int n, double a[], double x )
 2 {
 3    int i=0,j=0;
 4    double sum=0,x_n=1;;
 5    for(i=0;i<=n;i++)
 6    {
 7       sum += a[i] * x_n;
 8       x_n *= x;
 9    }
10    return sum;
11 }

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

转载请注明出处,欢迎邮件交流:[email protected]

猜你喜欢

转载自www.cnblogs.com/Amedeo/p/9079237.html