C语言算法检验 2.9题

来源:互联网 发布:淘宝买家好评 编辑:程序博客网 时间:2024/06/05 04:42

又一道算法检验,简直可怕;

笔者又加了系统自带的pow进行了测试

#include<stdio.h>#include<stdlib.h>#include<time.h>#include<math.h>double A,B,C;double algorithm1_pre(double X,int N){double temp=1;for(int i=0;i<N;i++){temp=temp*X;//printf("every temp:%f\n",temp);}return temp;}double algorithm1(double X,int N){double temp=0;for(int i=0;i<N+1;i++){temp+=algorithm1_pre(X,double(i));//printf("every temp:%f\n",temp);}return temp;}int IsEven(int N){if(N%2==0)return 1;if((N+1)%2==0)return 0;else printf("error");}double algorithm2_pre(double X,int N){if(N==0)return 1;if(IsEven(N))return algorithm2_pre(X*X,N/2);else return algorithm2_pre(X*X,N/2)*X;}double algorithm2(double X,int N){double temp2=0;for(int i=0;i<N+1;i++){temp2+=algorithm2_pre(X,i);//printf("every 2 temp:%f\n",temp2);}return temp2;}double algorithm3_pre(double X,int N){return pow(X,N);}double algorithm3(double X,int N){double temp=0;for(int i=0;i<N+1;i++){temp+=algorithm3_pre(X,i);//printf("every temp:%f\n",temp);}return temp;}double test1(double X,int N){   clock_t start,finish;   double duration=0.0;   start=clock();    /* 插入你的代码*/    A=algorithm1(X,N);   //printf("your result%f\n",algorithm1(X,N));   finish = clock();   duration = (double)(finish - start)/ CLOCKS_PER_SEC;  // printf( "%d turn Time is ",i+1);  // printf( "%f seconds\n",duration );   return duration;}double test2(double X,int N){clock_t start,finish;   double  duration=0.0;   start=clock();    /* 插入你的代码*/    //printf("your result2\n%f\n",algorithm2(X,N));   B=algorithm2(X,N);   finish = clock();   duration = (double)(finish - start)/ CLOCKS_PER_SEC;  // printf( "%d turn Time is ",i+1);  // printf( "%f seconds\n",duration );   return duration;}double test3(double X,int N){   clock_t start,finish;   double duration=0.0;   start=clock();    /* 插入你的代码*/    C=algorithm3(X,N);   //printf("your result%f\n",algorithm1(X,N));   finish = clock();   duration = (double)(finish - start)/ CLOCKS_PER_SEC;  // printf( "%d turn Time is ",i+1);  // printf( "%f seconds\n",duration );   return duration;}void main(){double X;int N;printf("please input the X:");scanf("%lf",&X);printf("please input the N:");scanf("%d",&N);printf("the time is %f seconds\n",test1(X,N));printf("the A sum is : %f\n",A);printf("the time2 is %f seconds\n",test2(X,N));printf("the B sum is : %f\n",B);printf("the time3 is %f seconds\n",test3(X,N));printf("the C sum is : %f\n",C);system("pause");}


PO出结果,非常可怕

这是 2.0,到9的结果

please input the X:2.0
please input the N:9
the time is 0.000000 seconds
the A sum is : 1023.000000
the time2 is 0.000000 seconds
the B sum is : 1023.000000
the time3 is 0.000000 seconds
the C sum is : 1023.000000
请按任意键继续. . .

这是 2.0,到99的结果

please input the X:2.0
please input the N:99
the time is 0.000000 seconds
the A sum is : 1267650600228229400000000000000.000000
the time2 is 0.000000 seconds
the B sum is : 1267650600228229400000000000000.000000
the time3 is 0.000000 seconds
the C sum is : 1267650600228229400000000000000.000000
请按任意键继续. . .

这是 2.0,到999的结果

please input the X:2.0
please input the N:999
the time is 0.000000 seconds
the time2 is 0.000000 seconds
the time3 is 0.000000 seconds
请按任意键继续. . .

这是 2.0,到2999的结果

please input the X:2.0
please input the N:2999
the time is 0.344000 seconds
the time2 is 0.000000 seconds
the time3 is 0.000000 seconds
请按任意键继续. . .

这是 2.0,到6999的结果

please input the X:2.0
please input the N:6999
the time is 3.103000 seconds
the time2 is 0.020000 seconds
the time3 is 0.000000 seconds
请按任意键继续. . .

这是 2.0,到9999的结果

please input the X:2.0
please input the N:9999
the time is 6.961000 seconds
the time2 is 0.030000 seconds
the time3 is 0.010000 seconds
请按任意键继续. . .

测试个上万的21314

please input the X:2.0
please input the N:21314
the time is 35.495000 seconds
the time2 is 0.062000 seconds
the time3 is 0.032000 seconds
请按任意键继续. . .

就到这里拉