多项式求和
【摘要】
∑=i∗xi∑=i∗xi
#include <stdio.h>
#include <time.h>
#include <math.h>
#define MA...
∑=i∗xi
#include <stdio.h>
#include <time.h>
#include <math.h>
#define MAXN 10
#define MAXK 1e7
clock_t start, stop;
double duration;
double f1(int n, double a[], double x);//直接的方法
double f2(int n, double a[], double x);//从里面到外面的方法
int main()
{
int i;
double a[MAXN], sum1, sum2;
for(i=0; i<MAXN; i++)
a[i] = (double)i;
start = clock();
for(i=0; i<MAXK; i++)
sum1=f1(MAXN-1, a, 1.1);
stop = clock();
duration = ((double)(stop-start))/CLK_TCK/MAXK;
printf("sum1 = %f\n", sum1);
printf("ticks1 = %f\n",(double)(stop-start));
printf("duration = %6.2e\n\n\n", duration);
start = clock();
for(i=0; i<MAXK; i++)
sum2=f2(MAXN-1, a, 1.1);
stop = clock();
duration = ((double)(stop-start))/CLK_TCK/MAXK;
printf("sum2 = %f\n", sum2);
printf("ticks2 = %f\n",(double)(stop-start));
printf("duration = %6.2e\n", duration);
return 0;
}
double f1(int n, double a[], double x)
//算法复杂度( 1+2+3...+n=(n^2+n)/2) 其实就是算乘法的次数,Tn=C1n^2+C2n
{
double sum=0;
int i;
for(i=0; i<=n; i++)
{
sum += (a[i]*pow(x,i));//这里+=后面加个大括号更保险
}
return sum;
}
double f2(int n, double a[], double x)
//算法复杂度n Tn=C*n
{
double sum=a[n];
int i;
for(i=n; i>0; i--)//注意这里是>0
{
sum = a[i-1] + sum*x;//这里也巧妙
}
return sum;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。
原文链接:recclay.blog.csdn.net/article/details/77870467
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)