1.for循环实现
#include <stdio.h>
#include <time.h> clock_t start, stop;
double duration; void printfN(); int main(){
int N;
printf("Pleade enter a number: ");
scanf("%d", &N);
start = clock();
printfN(N);
stop = clock();
duration = ((double)(stop - start))/CLK_TCK;
printf("duration = %f\n",duration);
return ;
} void printfN(int N){
int i;
for(i=; i<=N; i++){
printf("%d\n",i);
}
}
2.递归实现(有问题,思考——算法的执行效率与输入规模的关系)
#include <stdio.h>
#include <time.h> void printfN(); clock_t start, stop;
double duration; int main(){
int N;
printf("Please enter a number:");
scanf("%d", &N);
start = clock();
printfN(N);/*12000程序无法运行*/
stop = clock();
duration = ((double)(stop - start))/CLK_TCK;
printf("duration = %f\n",duration);
return ;
} void printfN(N){
if(N){
printfN(N-);
}
printf("%d\n",N);
}