“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

I. Problem description

Floating in the vast expanse of Galaxy X are man-made “bombs” of Planet X that serve as cosmic landmarks. Each bomb can be set how many days to go off.

For example, if the Alpha bomb was placed on November 1, 2015 and timed on November 1, 2015, and timed on 15 days, it was in the day, then it exploded on January 16, 2015.

There is a beta bomb, placed on November 9, 2014, timed to 1000 days, please calculate the exact date of its explosion.

Output the date in the format YYYY-MM-DD, i.e., 4-digit year, 2-digit month, and 2-digit date. For example, the 2015-02-19

Two, the title requirements

inspection

1. Date 2. The recommended time is 15 to 25 minutesCopy the code

Operating limits

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

Third, problem analysis

For the above problem, it is given that the current time is November 9, 2014, and the date is 1000 days after the solution. For months, we use a one-dimensional array to store the days of 12 months in advance, with February defaulting to 28 days.

A [13] =,31,28,31,30,31,30,31,31,30,31,30,31 {0}; // Write the monthCopy the code

For the year, the first judgment is not leap year, according to the leap year rule “four years a leap, 100 years do not leap, 400 years a leap”, the year meets one of the following conditions, it is a leap year.

(1) Divisible by 4 but not by 100 (e.g., 2004 was a leap year but 1900 was not) (2) divisible by 400 (e.g., 2000 was a leap year)Copy the code

If it is a leap year, February has 29 days, but not a leap year, February has 28 days.

For the number of days, there are 1000 days. The initial number of days is 9 days. When the number of days meets the requirement of the month, the month is added by one, and the number of days becomes one. If the number of days in a month is greater than 12, you need to add one to the year.

Four, coding implementation

#include<iostream>
#include<math.h>
using namespace std;
int main(a)
{
	int y=2014,m=11,d=9,i,n=1000;// Define the initial variable
	int a[13] = {0.31.28.31.30.31.30.31.31.30.31.30.31};// Write the month
	for(i=1; i<=n; i++) { d++;/ / number of + 1
		if(y%400= =0||y%4= =0&&y%100! =0)// Check whether it is a leap year
			a[2] =29;// If it is a leap year, February becomes 29 days, otherwise unchanged
		else
			a[2] =28;
		if(m>12)// If the month is greater than 12
		{
			y++;/ / + 1 year
			m=1;// The month becomes 1
		}
		if(d>a[m])
		{
			m++;// Month plus one
			d=1;// The number of days becomes one
		}
	}
	cout<<y<<"";// Due to output format problems
	if(m<10)// Check whether the month is in double digits
	{
		cout<<"0"<<m<<"";/ / output
	}
	else	
		cout<<m<<"";
	if(d<10)// Check whether the number of days is in two digits
	{
		cout<<"0"<<d;/ / output
	}
	else	
		cout<<d;
	return 0;
}
Copy the code

5. Output results