HORNER(霍纳)法则的C实现以及算法比较 2.10

来源:互联网 发布:linux删除多个文件命令 编辑:程序博客网 时间:2024/05/16 10:58

这个是2.10题的答案

double algorithm4_horner(double X,int N){double Poly = 0;for(int i = N; i> = 0; i--){聚=聚* X + 1;// printf(“every temp:%f \ n”,temp);}返回聚}

代码很长,可以直接去看结果

请输入X:2.0,
请输入N:9999
,time2为0.024000秒
,time3为0.012000秒
,time4 为0.000000秒
请输入N:99999
,time2为0.412000秒
,time3为0.190000秒
,time4为0.040000秒
请输入N:999999
时间2为4.947000秒
,time3 为2.774000秒,time4为0.346000秒

请输入N:5211314
,time2为29.883000秒
,time3为17.617000秒
,time4为1.811000秒
请输入N:


这是代码

的#include <stdio.h>中的#include <stdlib.h>中的#include <time.h>中的#include <math.h>中双A,B,C,D;double algorithm1_pre(double X,int N){double temp = 1;for(int i = 0; i <N; i ++){温度=温度* X;// printf(“every temp:%f \ n”,temp);}回温}double algorithm1(double X,int N){double temp = 0;for(int i = 0; i <N + 1; i ++){温度+ = algorithm1_pre(X,双(I));// printf(“every temp:%f \ n”,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))返回algorithm2_pre(X * X,N / 2);否则返回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){返回pow(X,N);}double algorithm3(double X,int N){double temp = 0;for(int i = 0; i <N + 1; i ++){温度+ = algorithm3_pre(X,I);// printf(“every temp:%f \ n”,temp);}回温}double algorithm4_horner(double X,int N){double Poly = 0;for(int i = N; i> = 0; i--){聚=聚* X + 1;// printf(“every temp:%f \ n”,temp);}返回聚}double test1(double X,int N){   clock_t开始,完成;   双持续时间= 0.0;   开始=时钟();    / *插入你的代码* /    A =算法1(X,N);   // printf(“你的结果%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);   返回时间;}double test2(double X,int N){clock_t开始,完成;   双持续时间= 0.0;   开始=时钟();    / *插入你的代码* /    // printf(“your result2 \ n%f \ n”,algorithm2(X,N));   B =算法2(X,N);   finish = clock();   duration =(double)(finish-start)/ CLOCKS_PER_SEC;  // printf(“%d turn Time is”,i + 1);  // printf(“%f seconds \ n”,duration);   返回时间;}double test3(double X,int N){   clock_t开始,完成;   双持续时间= 0.0;   开始=时钟();    / *插入你的代码* /    C = algorithm3(X,N);   // printf(“你的结果%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);   返回时间;}double test4(double X,int N){   clock_t开始,完成;   双持续时间= 0.0;   开始=时钟();    / *插入你的代码* /    D = algorithm4_horner(X,N);   // printf(“你的结果%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);   返回时间;}void main(){双X;int N;printf(“请输入X:”);的scanf( “%LF”,&X);printf(“请输入N:”);的scanf( “%d个”,&N);// printf(“time is%f seconds \ n”,test1(X,N));// printf(“A sum is:%f \ n”,A);printf(“time2为%f seconds \ n”,test2(X,N));// printf(“B sum is:%f \ n”,B);printf(“time3为%f seconds \ n”,test3(X,N));// printf(“C sum is:%f \ n”,C);printf(“time4为%f seconds \ n”,test4(X,N));// printf(“D sum is:%f \ n”,D);系统( “暂停”);}

用到HORNER法则,把百度百科,搬过来展示下基本https://baike.baidu.com/item/%E9%9C%8D%E7%BA%B3%E6%B3%95%E5%88%99 / 4822190?FR =阿拉丁



原创粉丝点击