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 }
  • 相关阅读:
    Nodejs:fs模块 & rimraf模块
    Nodejs:简单的脚手架(一)
    Nodejs:Glob对象
    Nodejs:Path对象
    后台系统组件:一丶bootstrap table
    关于Fekit的安装与测试
    Web存储-Web Storage
    【python】基础入门
    排除正则
    js正则处理千分位
  • 原文地址:https://www.cnblogs.com/LinSL/p/7473591.html
Copyright © 2011-2022 走看看