首页 > 其他 > 详细

1.1

时间:2015-09-03 16:33:39      阅读:239      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <time.h>
#include <math.h>
#define M 1000

double f2(int n,double a[], double x);
double f1(int n, double a[], double x);

int main(void) {
    double a[101];
    double ans,duration;
    clock_t start,end;
    a[0] = 1;
    for (int i = 1; i<101; i++) a[i] = (double)1/i;
    start = clock();
    for (int i = 0; i<M; i++) ans = f1(101, a, 1.1);
    end = clock();
    duration = ((double)(end - start)/CLOCKS_PER_SEC);
    printf("time of f1 = %f\n",duration);
    printf("result:%f\n",ans);
    start = clock();
    for (int i = 0; i<M; i++) ans = f2(101, a, 1.1);
    end = clock();
    duration = ((double)(end - start)/CLOCKS_PER_SEC);
    printf("time of f2 = %f\n",duration);
    printf("result:%f\n",ans);
    return 0;
}


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

 

1.1

原文:http://www.cnblogs.com/sjdeak/p/4780254.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!