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

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);
}
上一篇:python 发送附件至邮箱


下一篇:ScheduledThreadPoolExecutor源码分析-你知道定时线程池是如何实现延迟执行和周期执行的吗?