zoukankan      html  css  js  c++  java
  • 4-2 多项式求值

    本题要求实现一个函数,计算阶数为n,系数为a[0] ... a[n]的多项式f(x)=∑ni=0(a[i]*xi)f(x)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
    ", 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 sum;
        if(n>=0)
          sum=a[n]*pow(x,n)+f(n-1,a,x);
       return sum;
    }

  • 相关阅读:
    常见问题
    查询
    多对多关系
    prototype & __proto__
    new operator
    用户
    express.Router
    Express 应用生成器
    LeanCloud
    npm常用命令
  • 原文地址:https://www.cnblogs.com/entrepre/p/5249140.html
Copyright © 2011-2022 走看看