首页 > 试题广场 >

程序运行时间(15)

[编程题]程序运行时间(15)
  • 热度指数:18722 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
要获得一个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和C2。注意两次获得的时钟打点数肯定不相同,即C1 < C2,并且取值在[0, 107]


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

输入

123 4577973

输出

12:42:59
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) 
{
    int k,c1,c2,i,a,b,c;
 cin >> c1 >> c2; 
    const int ctk = 100;
    k = (int)(1.0 * (c2 - c1) / ctk + 0.5);//四舍五入的关键 
 a = k / 3600;
 b = (k % 3600) / 60; 
 c = k % 60;
 printf("%02d:%02d:%02d",a,b,c);
    return 0;
}
// 如何将浮点数 f 四舍五入为整形数  利用 (int)(f + 0.5) 来实现 

发表于 2017-08-18 22:26:56 回复(0)
#include<bits/stdc++.h>
int main(){
    int f,s;
    scanf("%d%d",&f,&s);
    int t=floor((s-f)/100.0+0.5);
    printf("%02d:%02d:%02d\n",t/3600,t%3600/60,t%60);
    return 0;
}




发表于 2015-07-24 11:16:57 回复(0)

python solution

c1, c2 = map(int, input().split())
#注意这里要四舍五入
seconds = round((c2 - c1) / 100)
#下面这两行是求对应的时、分、秒
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
#格式化输出
print (":".join(map(lambda c: str(c).rjust(2, "0"), [h, m, s])))
编辑于 2017-11-17 10:39:00 回复(1)
import java.util.Scanner;
public class Main{
	private static final double CLK_TCK = 100.0;
	private static final int MINUTE = 60;
	private static final int HOURS = 3600;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int c1 = in.nextInt();
		int c2 = in.nextInt();
		double time = (c2-c1)/CLK_TCK;
		int h = (int)time/HOURS;
		time %= HOURS;
		int m = (int)time/MINUTE;
		time %= MINUTE;
		System.out.printf("%02d:%02d:%02.0f\n",h,m,time);
	}
}

发表于 2016-06-10 19:13:24 回复(0)

#include<iostream>
using namespace std;
 
// PAT乙级真题程序运行时间(15)
 
intmain()
{
    longintC1,C2;
    doubletime;
    inthours,minutes,seconds;
    intCLK_TCK = 100;
    cin>>C1>>C2;
    time = (C2 - C1) / CLK_TCK;
    hours = (int)(time / 3600);
    minutes = (int)(time / 60- hours * 60);
    seconds = (int)(time - hours * 3600- minutes * 60);
    if((C2-C1)%100>=50)
        seconds = seconds+1;
    cout<<hours/10<<hours%10<<":"<<minutes/10<<minutes%10<<":"<<seconds/10<<seconds%10;
    return0;
}

发表于 2017-01-15 17:01:38 回复(0)

分析

  1. 范围检查, 不超过int范围,所以用int就行。
  2. 计算时需要考虑四舍五入,考虑一个最简单的。强转double类型数据会向下舍入。例如:
    double x = 1.6;
    int y = (int)x; //y = 1

基于这个考虑,可以在x基础上加0.5,再向int舍入,即可达到四舍五入的目的。

    double x = 1.6;
    int y = (int)(x+0.5); //y = 2
  1. 时间段,如何化为小时:分钟:秒?最容易想到的是直接算:

小时=时间段/3600秒;
分钟=(时间段-小时 * 3600)/60分;
秒=(时间段-小时 * 3600-分钟*60)。

从而得到结果。还有一种很好的方法,利用取余(取模)%运算。

小时 = 时间段/3600;
分钟: 时间段先对3600取余,这个操作会将3600的整数倍消去(消去小时元素),也即对应上面这个操作(时间段-小时 * 3600),得到剩余再除以60,得到分钟数。
秒: 同上理由,本需要先对3600取余(消去小时),在对60取余(消去分钟),但实际可以简化成:只对60取余,则同时会消去小时的成分(不需要先对3600取余,再对60取余,减少步骤)则剩余秒的成分,即得到秒。

/*
 * app=PAT-Basic lang=c++
 * https://pintia.cn/problem-sets/994805260223102976/problems/994805295203598336
 */
#include <cstdio>
using namespace std;
const double CLK_TCK = 100;
int main()
{
    int c1, c2, time;
    scanf("%d%d",&c1,&c2);
    time = (int)((c2 - c1) / CLK_TCK + 0.5);
    printf("%02d:%02d:%02d",time/3600,(time%3600)/60,time%60);
    return 0;
}
编辑于 2019-12-02 13:46:17 回复(0)
#include<iostream>
using namespace std;
int main(){
    int a, b,c, m[3];
    cin >> a >> b;
    c = (b-a+50)/100;
    for (int i = 0; i < 3; i++){
        m[i] = c % 60;
        c /= 60;
    }
    cout << m[2]/10 <<m[2]%10<< ":" << m[1]/10<<m[1]%10 << ":" << m[0]/10 <<m[0]%10<< endl;
    system("pause");
    return 0;
}

发表于 2019-04-27 23:41:09 回复(0)
#include <stdio.h>
int main(){
 long C1, C2, time, hms[3] = {0};
 scanf("%ld %ld", &C1, &C2);
 time = float(C2 - C1 + 50) / 100;
 printf("%02d:%02d:%02d",time / 3600 % 60, time / 60 % 60, time % 60);
 return 0;
}

发表于 2017-08-09 00:49:42 回复(0)
import java.util.Scanner;

/**
 * 程序运行时间
 * 题目描述
 * 要获得一个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和C2。注意两次获得的时钟打点数肯定不相同,即C1 < C2,并且
 * 取值在[0, 107]
 * 输出描述:
 * 在一行中输出被测函数运行的时间。运行时间必须按照“hh:mm:ss”(即2位的“时:分:秒”)格式
 * 输出;不足1秒的时间四舍五入到秒。
 * 输入例子:
 * 123 4577973
 * 输出例子:
 * 12:42:59
 *
 * @author shijiacheng
 * @date 2018/2/1
 */
public class B1016ProgramRunTime {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int C1 = sc.nextInt();
        int C2 = sc.nextInt();

        float temp = (C2 - C1) * 1.0f / 100;
        int time = Math.round(temp);
        int hour = time / 3600;
        time = time - hour * 3600;
        int min = time / 60;
        time = time - min * 60;
        int second = time;

        StringBuilder sb = new StringBuilder();
        if (hour < 10) {
            sb.append(0);
        }
        sb.append(hour);
        sb.append(":");
        if (min < 10) {
            sb.append(0);
        }
        sb.append(min);
        sb.append(":");
        if (second < 10) {
            sb.append(0);
        }
        sb.append(second);
        System.out.println(sb.toString());
    }
}
发表于 2018-02-01 22:08:17 回复(0)
#include<iostream>
#define CLK_TCK 100
usingnamespacestd;
 
intmain()
{
    intC1,C2;
    cin>>C1>>C2;
    inttime=(C2-C1)/100;
    inth,m,s;
    h=time/3600;
    m=time%3600/60;
    s=time%3600%60;
    cout<<(h<10?"0":"")<<h<<":"<<(m<10?"0":"")<<m<<":"<<(s<10?"0":"")<<s<<endl;
     
    return0;
}

发表于 2019-01-17 15:09:39 回复(0)
try:
    while True:
        digitList = list(map(int,input().split()))
        secondsNum = int((digitList[1]-digitList[0])/100+0.5)
        hours,secondsNum = divmod(secondsNum,3600)
        minutes,seconds = divmod(secondsNum,60)
        print("%02d:%02d:%02d"%(hours,minutes,seconds))
except Exception:
    pass
编辑于 2018-09-21 17:43:51 回复(0)
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#define CLK_TCK 100
using namespace std;

int main()
{
    int c1, c2;
    //输入
/*
    {
        FILE *fp;
        fp = fopen("Text.txt", "r");
        fscanf(fp, "%d %d", &c1, &c2);
    }
*/
    cin >> c1 >> c2;
    int length = (c2 - c1) / CLK_TCK;
    int hour = length / 3600;
    int minute = length % 3600 / 60;
    int second = ((double)(c2 - c1) / CLK_TCK) - ((c2 - c1) / CLK_TCK) >= 0.5 ? (length % 3600 % 60 + 1) : (length % 3600 % 60);
    printf("%02d:%02d:%02d", hour, minute, second);

    return 0;
}
发表于 2018-06-25 16:05:03 回复(0)
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
   
    class Program
    {
        static void Main(string[] args)
        {
            
            string []array = Console.ReadLine().Split(' ');
            long a = long.Parse(array[0]);
            long b = long.Parse(array[1]);
            long c = b - a;
            long d = c / 100;
            long e = c % 100;
            d=(e>49)?d+1:d;
            int hh, mm, ss;
            ss =(int) d % 60;
            d = d / 60;

            mm = (int)d % 60;
            d = d / 60;

            hh = (int)d % 60;
            if (hh < 10 && mm < 10 && ss < 10)
                Console.WriteLine("0{0}:0{1}:0{2}", hh, mm, ss);
            else if (hh < 10 && mm < 10)
                Console.WriteLine("0{0}:0{1}:{2}", hh, mm, ss);
            else if (hh < 10 && ss < 10)
                Console.WriteLine("0{0}:{1}:0{2}", hh, mm, ss);
            else if (mm < 10 && ss < 10)
                Console.WriteLine("{0}:0{1}:0{2}", hh, mm, ss);
            else if (hh<10)
                Console.WriteLine("0{0}:{1}:{2}", hh, mm, ss);
            else if (mm < 10)
                Console.WriteLine("{0}:0{1}:{2}", hh, mm, ss);
            else if (ss < 10)
                Console.WriteLine("{0}:{1}:0{2}", hh, mm, ss);
            else
                Console.WriteLine("{0}:{1}:{2}",hh,mm,ss);
            
             
        }
    }
}


发表于 2017-04-25 11:20:33 回复(0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
importjava.math.BigDecimal;
importjava.util.Scanner;
publicclassMain {
    publicstaticvoidmain(String[] args){
        Scanner sc = newScanner(System.in);
        intstart = sc.nextInt();
        intend = sc.nextInt();
        doubletime = (end-start)/100f;
        inthh = (int) (time/3600);
        intmm = (int) ((time-3600*hh)/60);
        doubles = time-hh*3600-mm*60;
        longss =  Math.round(s);
        String str = (hh<10?"0"+hh:hh)+":"+ (mm<10?"0"+mm:mm)+":"+ (ss<10?"0"+ss:ss );
        System.out.print(str);
    }
}

发表于 2016-08-09 16:28:43 回复(0)
#include<math.h>
#include<stdio.h>
int main() {
	int a, b;
	scanf("%d%d", &a, &b);
	int t = floor((b - a) / 100.0 + 0.5);
	printf("%02d:%02d:%02d\n", t / 3600, t % 3600 / 60, t % 60);
	return 0;
}
编辑于 2016-02-25 11:25:53 回复(0)
//这道题注意输出格式就好了

import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int C1 = sc.nextInt();
		int C2 = sc.nextInt();
		int seconds = (C2 - C1)/100;
		int  r = (C2 - C1)%100;
		if(r>=50) seconds ++;
		int hour,minute,second;
		hour = seconds/3600;
		minute = (seconds%3600)/60;
		second = seconds - hour*3600 -minute*60;
		System.out.printf("%02d",hour);
		System.out.print(":");
		System.out.printf("%02d",minute);
		System.out.print(":");
		System.out.printf("%02d",second);
	}

}


发表于 2015-10-24 15:40:16 回复(0)
#include<stdio.h>
int tt,mm,ss;
int t1,t2,time;
int main(void)
{
    scanf("%d %d",&t1,&t2);
    time=(t2-t1)/100;
    tt=time/3600;
    mm=(time-tt*3600)/60;
    ss=time%60;
    printf("%02d:%02d:%02d",tt,mm,ss);
}
发表于 2015-11-24 11:48:54 回复(0)
#include<iostream>
#define CLK_TCK 100
using namespace std;
void print(int num)
{
    if (num < 10)
        cout << "0" << num;
    else
        cout << num;
}
int main()
{
    int begin, end;
    cin >> begin >> end;
    int clocksum = end - begin;
    int seconds = clocksum / CLK_TCK;
    int yushu = clocksum % 100;
    if (yushu >= 50)
        seconds++;
    int hours = seconds / 3600;
    int minutes = (seconds - hours * 3600) / 60;
    seconds = seconds - hours * 3600 - minutes * 60;
    print(hours);
    cout << ":";
    print(minutes);
    cout << ":";
    print(seconds);
    cout << endl;
    return 0;
}
编辑于 2015-07-10 16:51:49 回复(1)
#include<bits/stdc++.h>
using namespace std;

int main(){
	double n,m;
	while(cin>>n>>m){
		int s=round((m-n)/100);
		int h=s/3600;
		int m=(s-h*3600)/60;
		s-=h*3600+m*60;
		printf("%02d:%02d:%02d",h,m,s);
	}
	return 0;
}

发表于 2022-11-02 11:56:40 回复(0)
#include<stdio.h>
#define CLK_TCK 100
int main()
{
    long c1,c2,second;
    int hh,mm,ss;
    scanf("%ld%ld",&c1,&c2);
    second=(int)(1.0*(c2-c1)/CLK_TCK+0.5);
    hh=second/3600;
    mm=(second%3600)/60;
    ss=(second%3600)%60;
    printf("%02d:%02d:%02d",hh,mm,ss);
}
发表于 2021-02-05 21:22:07 回复(0)