1.1

#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;
}

 

posted @ 2015-09-03 15:31  sjDeak  阅读(254)  评论(0)    收藏  举报