C语言算法检验 2.9题

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

笔者又加了系统自带的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
请按任意键继续. . .

就到这里拉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值