码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构-9阶多项式

时间:2018-05-25 21:05:48      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:start   i++   math.h   return   stdio.h   tick   char   math   多项式   

#include<stdio.h>
#include<time.h>
#include<math.h>
clock_t start, stop;
double duration;
#define MAXN 10 //多项式最大项数,即多项式阶数+1

double f1(int n, double a[], double x)
{
int i;
double p = a[0];
for (i = 1; i <= n; i++)
p += (a[i]*pow(x, i));
return p;

}

double f2(int n, double a[], double x)
{
int i;
double p = a[n];
for (i = n; i > 0; i++)
p = a[i - 1] + x*p;
return p;

}

int main()
{
int i;
double a[MAXN];
for (i = 0; i < MAXN; i++)
a[i] = (double)i;
start = clock();
f1(MAXN - 1, a, 1.1);
stop = clock();
duration = ((double)(stop - start)) / CLK_TCK;
printf("ticks1= %f\n", (double)(stop - start));
printf("duration1 = %6.2e\n", duration);


start = clock();
f1(MAXN - 1, a, 1.1);
stop = clock();
duration = ((double)(stop - start)) / CLK_TCK;
printf("ticks2= %f\n", (double)(stop - start));
printf("duration2 = %6.2e\n", duration);
getchar();
return 0;
}

数据结构-9阶多项式

标签:start   i++   math.h   return   stdio.h   tick   char   math   多项式   

原文地址:https://www.cnblogs.com/minTTremor/p/9090206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!