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

1.1

时间:2015-09-03 16:35:06      阅读:153      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!