当前位置:网站首页>Time display of the 12th Blue Bridge Cup

Time display of the 12th Blue Bridge Cup

2022-06-13 05:00:00 Clown clown clown

 Insert picture description here
Examination site : simulation .
For time , Common methods are modulo and division .
Common formula :

hour = time / 3600
min = time % 3600 / 60
sec = time % 3600 % 60

notes : Understanding of modular operation , Equivalent to filtering out unwanted seconds .
for instance min = time % 3600 / 60; It is equivalent to asking for the number of minutes , Filter out the hours that belong to ( subtract ), Then divide the remaining seconds by 60, Get the minutes .

Ideas :
This problem is a little distorted . Since there is no need to display year, month and day , Therefore, the number of seconds given should be filtered out first . namely time % (24 * 60 * 60)

And a little bit more , It's not the number of seconds , It's milliseconds , So first divide by 1000 Count seconds , Then carry out a series of operations .

Code :

#include <iostream>

using namespace std;

long long t;

int main()
{
    
	cin >> t;
	t /= 1000;// hold ms become s
	t %= (24 * 60 * 60);// Filter out the seconds belonging to month, year and day 
	// Three formulas 
	int h = t / 3600;
	int m = t % 3600 / 60;
	int s = t % 3600 % 60;
	printf("%02d:%02d:%02d", h, m, s);
}

notes : Don't cycle every second to find the final hour,minute,second. There will be a series of mistakes .

原网站

版权声明
本文为[Clown clown clown]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280516403874.html