1.for循环实现

 1 #include <stdio.h>
 2 #include <time.h>
 3 
 4 clock_t start, stop;
 5 double duration;
 6 
 7 void printfN();
 8 
 9 int main(){
10     int N;
11     printf("Pleade enter a number: ");
12     scanf("%d", &N);
13     start = clock();
14     printfN(N);
15     stop = clock();
16     duration = ((double)(stop - start))/CLK_TCK;
17     printf("duration = %fn",duration);
18     return 0;
19 }
20 
21 void printfN(int N){
22     int i;
23     for(i=0; i<=N; i++){
24         printf("%dn",i);
25     }
26 }

2.递归实现(有问题,思考——算法的执行效率与输入规模的关系)

 1 #include <stdio.h>
 2 #include <time.h>
 3 
 4 void printfN();
 5 
 6 clock_t start, stop;
 7 double duration;
 8 
 9 int main(){
10     int N;
11     printf("Please enter a number:");
12     scanf("%d", &N);
13     start = clock();
14     printfN(N);/*12000程序无法运行*/
15     stop = clock();
16     duration = ((double)(stop - start))/CLK_TCK;
17     printf("duration = %fn",duration);
18     return 0;
19 }
20 
21 void printfN(N){
22     if(N){
23         printfN(N-1);
24     }
25     printf("%dn",N);
26 }

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!