比较求N阶多项式的算法比较

 

 

 

 

 

 

#include<stdio.h>
#include<math.h>
#include<time.h>

#define MAXK 1e6
/*
you can get the question fromE:\project\java_algorithm\C_Algorithem\algorithm01\week01\compareForAndRecursion demo2.bmp
or from web:http://www.icourse163.org/learn/ZJU-93001?tid=1002019005#/learn/content?type=detail&id=1002635001&cid=1002891006
*/

/*
implement this question using violence loop
the T(n)=O(n^2);
*/
double fun1(double x,int n){
    double sum=1.0;
    int i;
    for(i=1;i<=100;i++){
        sum+=pow(x,i)/i;
    }
    return sum;
}

/*
we can store x^i into a temp, every time
we only need to multiply i base on last time value.
T(n)=2n
*/
double fun2(double x,int n){
    double sum=1.0;
    double temp=1;
    int i;
    for(i=1;i<100;i++){
        temp=temp*x;
        sum=sum+temp/i;
    }
    return sum;
}


/*Just a main method used to test*/
void main(){
    int i;
    //start the time,use the second
    clock_t start,end;
    double duration;//used to stored top - end
    start=clock();
    for(i=0;i<MAXK;i++){
        fun1(1.1,100);
    }
    end=clock();
    duration=((double)(end-start))/CLK_TCK/MAXK;
    printf("every method fun1 using average time:%f\n",duration);

    start=clock();
    for(i=0;i<MAXK;i++){
        fun2(1.1,100);
    }
    end=clock();
    duration=((double)(end-start))/CLK_TCK/MAXK;
    printf("every method fun2 using average time:%f\n",duration);

    /*
    summary:sometimes,you can using temporary variable to reduce the T(n)
    */
}





转载于:https://www.cnblogs.com/yghjava/p/6575571.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值