zoukankan      html  css  js  c++  java
  • 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
    ",duration);
    18     return 0;
    19 }
    20 
    21 void printfN(int N){
    22     int i;
    23     for(i=0; i<=N; i++){
    24         printf("%d
    ",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
    ",duration);
    18     return 0;
    19 }
    20 
    21 void printfN(N){
    22     if(N){
    23         printfN(N-1);
    24     }
    25     printf("%d
    ",N);
    26 }
  • 相关阅读:
    C#中关于zip压缩解压帮助类的封装(转)
    MonoTouch的官网
    Android布局
    VS2010网站发布
    HTML5的PLACEHOLDER属性
    some np problem
    srm 578
    opencv 边缘算子
    Python扩展(pybind11混编)
    PyTorch之初级使用
  • 原文地址:https://www.cnblogs.com/LinSL/p/7473591.html
Copyright © 2011-2022 走看看