Rounding function round() (pat class B 1026 program running time)

To obtain the running time of a C language program, the common method is to call the header file time.h, which provides the clock() function, which can capture the time spent from the start of the program to the time when clock() is called. This unit of time is clock tick, that is, "clock tick". At the same time, there is also a constant CLK_TCK, which gives the number of clock ticks that the machine clock moves per second. Therefore, in order to obtain the running time of a function f, we only need to call clock() before calling f to obtain a clock count C1; after the execution of f is completed, call clock() to obtain another clock count C2; twice The obtained clock dots difference (C2-C1) is the clock dots consumed by the operation of f, and then divided by the constant CLK_TCK, the running time in seconds is obtained.

Here we may simply assume that the constant CLK_TCK is 100. Now given the number of clock ticks obtained twice before and after the function under test, please give the running time of the function under test.

Input format:

The input gives 2 integers C1 and C2 sequentially on one line. Note that the number of clock ticks obtained twice must be different, that is, C1 < C2, and the value is in [0,107].

Output format:

Outputs in one line the time the function under test takes to run. Elapsed times must be output in  hh:mm:ss(ie 2-digit  时:分:秒) format; fractions of a second are rounded to the nearest second.

Input sample:

123 4577973

Sample output:

12:42:59

A relatively basic topic, mainly mastered a rounding function round()

1. You need to import the <cmath> package, and you can round it directly by round()

#include <iostream>
#include <string>
#include <map>
#include<cmath>
#include <algorithm>
using namespace std;

int main(){
	int c1,c2;
	cin>>c1>>c2;
	float time = c2-c1;
	time = time/100;
	int s,m,h;
	h = time/3600;
	m = (time - 3600*h)/60;
	s = round((time - 3600*h-60*m));
	printf("%02d:%02d:%02d",h,m,s);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45721305/article/details/123423878