zoukankan      html  css  js  c++  java
  • 程序运行时间

    链接:http://www.nowcoder.com/pat/6/problem/4052

    题目描述

    要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所

    耗费的时间。这个时间单位是clock tick,即“时钟打点”。同时还有一个常数CLK_TCK,给出了机器时钟每秒所走的时钟打点数。于是为了获

    得一个函数f的运行时间,我们只要在调用f之前先调用clock(),获得一个时钟打点数C1;在f执行完成后再调用clock(),获得另一个时钟打点

    数C2;两次获得的时钟打点数之差(C2-C1)就是f运行所消耗的时钟打点数,再除以常数CLK_TCK,就得到了以秒为单位的运行时间。



    这里不妨简单假设常数CLK_TCK为100。现给定被测函数前后两次获得的时钟打点数,请你给出被测函数运行的时间。

    输入描述:

    输入在一行中顺序给出2个整数C1和C1。注意两次获得的时钟打点数肯定不相同,即C1 < C2,并且取值在[0, 107]



    输出描述:

    在一行中输出被测函数运行的时间。运行时间必须按照“hh:mm:ss”(即2位的“时:分:秒”)格式输出;不足1秒的时间四舍五入到秒。

    输入例子:

    123 4577973

    输出例子:

    12:42:59

    思路:题目很简单,只要做好”四舍五入“的计算,就可以了。
    ”四舍五入“:double a = 1.2; a += 0.5; floor[a] = 1;
          double b = 1.5; b += 0.5; floor[b] = 2;
    floor(x):取不大于x的最大整数(小于等于x的最大整数)
    ceil(x) : 取不小于x的最小整数(大于等于x的最小整数)
     1 #include "iostream"
     2 #include <iomanip>
     3 #include <string.h>
     4 #include <string>
     5 #include <vector>
     6 #include <cmath>
     7 #include <cctype>
     8 #include <algorithm>
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     int c1, c2;
    14     cin >>c1 >>c2;
    15     int c3;
    16     if(c1 > c2)
    17     {
    18         c3 = c1;
    19         c1 = c2;
    20         c2 = c3;
    21     }
    22     c3 = c2-c1;
    23     c3 = floor((c3*1.0/(100*1.0))+0.5);
    24     int h = c3/3600;
    25     int m = (c3-h*3600)/60;
    26     int s = (c3-h*3600-m*60);
    27     if(h < 10)
    28     {
    29         cout <<0 <<h <<":";
    30     }
    31     else cout <<h <<":";
    32     if(m < 10)
    33     {
    34         cout <<0 <<m <<":";
    35     }
    36     else cout <<m <<":";
    37     if(s < 10)
    38     {
    39         cout <<0 <<s <<endl;
    40     }
    41     else cout <<s <<endl;
    42     return 0;
    43 }
  • 相关阅读:
    IDEA的Debug详解
    websocket学习(转载)
    Shiro授权及注解式开发
    Redis分布式缓存安装和使用
    JEESZ-SSO解决方案
    英语是学习Java编程的基础吗
    深入分析 ThreadLocal 内存泄漏问题
    这些JVM命令配置参数你知道吗?
    安全开发Java动态代理
    学java编程软件开发,非计算机专业是否能学
  • 原文地址:https://www.cnblogs.com/mtc-dyc/p/4624172.html
Copyright © 2011-2022 走看看