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 }

 

转载于:https://www.cnblogs.com/LinSL/p/7473591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值