美文网首页
6-2 多项式求值

6-2 多项式求值

作者: Dr_Jagger | 来源:发表于2018-04-05 14:12 被阅读0次

    6-2 多项式求值(15 分)

    问题描述.png

    函数接口定义:

    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;
    }
    
    /* 你的代码将被嵌在这里 */
    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
    

    相关文章

      网友评论

          本文标题:6-2 多项式求值

          本文链接:https://www.haomeiwen.com/subject/wszrhftx.html