Algorithm exercise 29 - "time display" of 2021 provincial competition of Blue Bridge Cup

Posted by dsoftnet on Sat, 12 Feb 2022 15:16:19 +0100

preface

Blue Bridge Cup 2021 provincial competition, programming problem (C + +)

1, Title Description

Xiaolan wants to cooperate with her friends to develop a time display website.

On the server, the friend has obtained the current time, which is expressed as an integer. The value is the number of milliseconds from 00:00:00 on January 1, 1970 to the current time.

Now, Xiaolan will display this time on the client. Xiaolan doesn't need to display the year, month and day. It only needs to display the hours, minutes and seconds, and milliseconds. It can be directly rounded off.

Given a time expressed as an integer, please output the hour, minute and second corresponding to this time.

Enter description

The input line contains an integer representing the time.

Output description

Output the current time represented by hour, minute and second. The format is HH:MM:SS, where HH represents hour, the value is 0 to 23, MM represents minute, the value is 0 to 59, SS represents second, and the value is 0 to 59. When the hour, minute and second are less than two digits, the leading 0 shall be supplemented.

Sample input and output

Example 1

input

46800999

output

13:00:00

Example 2

input

1618708103123

output

01:08:23

Scale and agreement of evaluation cases

For all evaluation cases, the given time is a positive integer no more than 10 ^ 18.

Operational limits

  • Maximum running time: 1s
  • Maximum running memory: 512M

2, Train of thought

The problem itself is not difficult, but it needs some patience. First convert the input time into seconds, and then decrease layer by layer from year, month to day, hour, minute and second. Start from the year and reduce the time to within the current year, and then start from the month and reduce the time to within the current month. Note the impact of leap year and non leap year on the number of days in the year and the impact of month. Based on the above ideas, we can finally find the answer.

3, Specific code

#include<bits/stdc++.h>
using namespace std;
int main()
{
	unsigned long long n;
	cin>>n;
	n=n/1000;  //Turn it into seconds first
	unsigned long long year1=366*24*60*60;
	unsigned long long year2=365*24*60*60;
	//Start with year
	int year=1971;
	while(1)
	{
		if((year%4==0&&year%100!=0)||year%400==0)  //leap year
		{
			if(n<year1)
			{
				break;
			}
			else if(n==year1)
			{
				cout<<"00:00:00"<<endl;
				return 0;
			}
			n-=year1;
		}
		else  //Non leap year
		{
			if(n<year2)
			{
				break;
			}
			else if(n==year2)
			{
				cout<<"00:00:00"<<endl;
				return 0;
			}
			n-=year2;
		}
		year++;
	}
	int month[13]={0};
	month[1]=31*24*60*60;
	month[2]=28*24*60*60;
	month[3]=31*24*60*60;
	month[4]=30*24*60*60;
	month[5]=31*24*60*60;
	month[6]=30*24*60*60;
	month[7]=31*24*60*60;
	month[8]=31*24*60*60;
	month[9]=30*24*60*60;
	month[10]=31*24*60*60;
	month[11]=30*24*60*60;
	month[12]=31*24*60*60;
	
	//Decrease from month
	int m=1;
	if((year%4==0&&year%100!=0)||year%400==0) //This year is a leap year, and February is specially treated
	{
		while(1)
		{
			if(m!=2)
			{
				if(n<month[m])  //Within this month
				{
					break;	
				}
				else if(n==month[m])  //It's just past this month
				{
					cout<<"00:00:00"<<endl;
					return 0;
				}
				else
				{
					n-=month[m];
					m++;
				}
			}
			else
			{
				if(n<month[m]+24*60*60)  //Within 2 months
				{
					break;
				}
				else if(n==month[m]+24*60*60)  //It's just past February
				{
					cout<<"00:00:00"<<endl;
					return 0;
				}
				else
				{
					n-=month[m];
					m++;
				}
			}
		}
	}
	
	//Decrease from day
	while(1)
	{
		if(n<24*60*60)  //Within this day
		{
			break;
		}
		else if(n==24*60*60)  //The time just starts the next day
		{
			cout<<"00:00:00"<<endl;
			return 0;
		}
		else
		{
			n-=24*60*60;
		}
	}
	
	//Look for it from hour, minute and second
	for(int i=0;i<=23;i++)
	{
		for(int j=0;j<=59;j++)
		{
			for(int k=0;k<=59;k++)
			{
				if(i*60*60+j*60+k==n)
				{
					printf("%02d:%02d:%02d",i,j,k);  //Supplement the leading 0. The default character width is 2
					return 0;
				}
			}
		}
	}
	return 0;
}

Topics: Algorithm