2015-The 6th Blue Bridge Cup Individual Provincial Competition (Software) Zhenti C University Group A 2. Galaxy Bomb

Title description

There are many X-star artificial "bombs" floating in the vast space of the X galaxy, which are used as road signs in the universe.
Each bomb can be set to explode in days.
For example, if the Alpha Bomb is placed on January 1, 2015 and the time is 15 days, it will explode on January 16, 2015.
There is a beta bomb, which was placed on November 9, 2014, with a timing of 1000 days. Please calculate the exact date when it exploded.

Please fill in the date in the format yyyy-mm-dd, which is a 4-digit year, 2-digit month, and 2-digit date. For example: 2015-02-19
please write in strict accordance with the format. No other words or symbols can appear.

analysis

A very simple question, I can use my basic mathematical knowledge to figure it out by hand is 2017-08-05, the following is a program exercise:

#include<iostream>
using namespace std;
int mon[13]={
    
    31,28,31,30,31,30,31,31,30,31,30,31};
bool leapy(int y){
    
    //判断是否是闰年,返回 bool 值
	if(y%4==0 && y%100!=0 || y%400==0)
		return true;
	return false;
}
int main(){
    
    
	bool isleapy=false;
	int y=2014,m=11,d=9,n=1000;
	for(int i=1;i<=n;i++){
    
    //学会这种一天一天前进判断的思想
		d++;
		if(m==2){
    
    
			if(isleapy){
    
    
				if(d>29){
    
    
					m++;
					d=1;
				}
			}
			else{
    
    
				if(d>28){
    
    
					m++;
					d=1;
				}
			}
		}
		else if(d>mon[m-1]){
    
    
			m++;
			d=1;
		}
		if(m>12){
    
    
			y++;
			m=1;
			isleapy=leapy(y);
		}
	}
	printf("%d-%02d-%02d\n",y,m,d);//不足两位时左侧补 0 补齐
	return 0;
}

Output: 2017-08-05

Guess you like

Origin blog.csdn.net/interestingddd/article/details/114835313