mirror of
https://github.com/renbaoshuo/202401-programming-assignments.git
synced 2024-11-27 08:26:20 +00:00
45 lines
803 B
Markdown
45 lines
803 B
Markdown
|
# 6-4 多项式求值
|
||
|
|
||
|
本题要求实现一个函数,计算阶数为`n`,系数为`a[0]` ... `a[n]`的多项式$$f(x)=\sum_{i=0}^{n}(a[i]\times x^i)$$ 在`x`点的值。
|
||
|
|
||
|
### 函数接口定义:
|
||
|
```c++
|
||
|
double f( int n, double a[], double x );
|
||
|
```
|
||
|
|
||
|
其中`n`是多项式的阶数,`a[]`中存储系数,`x`是给定点。函数须返回多项式`f(x)`的值。
|
||
|
|
||
|
### 裁判测试程序样例:
|
||
|
```c++
|
||
|
#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;
|
||
|
}
|
||
|
|
||
|
/* 你的代码将被嵌在这里 */
|
||
|
```
|
||
|
|
||
|
### 输入样例:
|
||
|
```in
|
||
|
2 1.1
|
||
|
1 2.5 -38.7
|
||
|
```
|
||
|
|
||
|
### 输出样例:
|
||
|
```out
|
||
|
-43.1
|
||
|
```
|