C_输入一个整数N,输出从0~N(算法思考)

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 = %f\n",duration);
18     return 0;
19 }
20 
21 void printfN(int N){
22     int i;
23     for(i=0; i<=N; i++){
24         printf("%d\n",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 = %f\n",duration);
18     return 0;
19 }
20 
21 void printfN(N){
22     if(N){
23         printfN(N-1);
24     }
25     printf("%d\n",N);
26 }

 

posted @ 2017-09-04 14:49  忍冬。  阅读(523)  评论(0编辑  收藏  举报