# 常用算法程序集（c/c++）笔记（一）

（一）一维多项式求值

Plvy.c程序

#include "stdafx.h" //visual studio 2017中的头文件
#include "stdio.h"
double plyv(double a[], int n, double x)
{
int i;
double u;
u = a[n - 1];
for (i = n - 2; i >= 0; i--)
u = u*x + a[i];
return (u);
}  //求值函数，主要思想是迭代
int main()
{
int i;
double a[7] = { -20.0,7.0,-7.0,-1.0,3.0,-5.0,2.0 };
double x[2] = { 0.9,-0.9 };
printf("\n");
for (i = 0; i <= 1; i++)
printf("x(%d)=%5.21f p(%d)=%13.7e\n)", i, x[i], i, plyv(a, 7, x[i])); //查找C的输出格式
printf("\n");
}

Plvy.cpp 程序

#include "stdafx.h"
#include <iostream>
using namespace std;
double plyv(double a[], int n, double x)
{
int i;
double u;
u = a[n - 1];
for (i = n - 2; i >= 0; i--)
u = u*x + a[i];
return (u);
}  //求值函数，主要思想是迭代
int main()
{
int i;
double a[7] = { -20.0,7.0,-7.0,1.0,3.0,-5.0,2.0 };
double x[6] = { 0.9,-0.9,1.1,-1.1,1.3,-1.3 };
for (i = 0; i <= 5; i++)
cout <<"x"<<"("<<i<<")"<<"="<< x[i] <<"\t"<<"p"<< "(" << i << ")" <<"="<< plyv(a, 7, x[i]) << endl;
return (0);
}

（二）总结与提升

cout <<"x"<<"("<<i<<")"<<"="<< x[i] <<"\t"<<"p"<< "(" << i << ")" <<"="<< plyv(a, 7, x[i]) << endl;

02-01