zoukankan      html  css  js  c++  java
  • PAT 1026

    1. 程序运行时间(15)

    时间限制
    200 ms
    内存限制
    65536 kB
    代码长度限制
    8000 B
    判题程序
    Standard
    作者
    CHEN, Yue
    要获得一个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

    题目就是给你一定的秒数让你转换成时分秒的形式,注意四舍五入,还有要求2位的形式输出。具体见代码

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    #include <string>
    
    using namespace std;
    int num;
    int c1,c2;
    int second,minute,hour;
    
    int main()
    {
        while(scanf("%d%d",&c1,&c2)!=EOF)
        {
              num=(c2-c1)/100;
              if((c2-c1)%100>=50)
                num++;
             hour=num/3600;
             num-=hour*3600;
             minute=num/60;
             num-=minute*60;
             second=num;
         printf("%02d:%02d:%02d
    ",hour,minute,second);
         }
        return 0;
    }
  • 相关阅读:
    docker 安装mysql
    Java web项目搭建系列之二 Jetty下运行项目
    Java web项目搭建系列之一 Eclipse中新建Maven项目
    Maven 添加其他Maven组件配置问题
    C# 中定义扩展方法
    Oracle 函数
    【Webservice】2 counts of IllegalAnnotationExceptions Two classes have the same XML type name
    Linux精简版系统安装网络配置问题解决
    Rsync 故障排查整理
    Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228773.html
Copyright © 2011-2022 走看看