[Blue Bridge Cup] Exam Training 2013 C++A Group Question 1 Gauss Diary

Gauss Diary 

    The great mathematician Gauss has a good habit: keep a diary anyway.

    There is a special place in his diary. He never indicates the year, month and day, but instead uses an integer, such as: 4210

    Later, people learned that that integer was the date, which indicated that that day was the day after Gauss was born. This may also be a good habit. It reminds the owner all the time: Days pass one day, how much time can be used to waste?

    Gauss was born: April 30, 1777.
    
    In the diary of an important theorem discovered by Gauss, it is marked: 5343, so it can be calculated that the day is: December 15, 1791.

    The diary on the day Gauss received his PhD is marked 8113   

    Please calculate the year, month, and day when Gauss received his Ph.D.

The format for submitting answers is: yyyy-mm-dd, for example: 1980-03-21

Please strictly follow the format and submit the answer through the browser.
Note: Only submit this date, do not write other additional content, such as: descriptive text.

Answer: 1799-07-16

 

Problem analysis

Method 1: You can use Excel to directly calculate the result, and you need to be careful.

Method 2: Programming calculation. Idea: Simulate from the given starting time one day to the next day, adding to the specified number and the result is the desired date. Then what needs to be dealt with is that in the process of adding the number of days, there will be a leap year, and the month will be rounded after the number of days of a month is added, and the month will continue to be rounded after the month reaches 12. Then judge these several situations . The number at the end of February in a leap year is not the same as that at the end of February in a non-leap year, so February also needs to be judged twice. There is another place, every time the number of days is added, d++ is correct, so when the judgment is made, d is one more than the last day of the month.

Finally, after the program is written, use the existing examples in the title to verify it, and you can get the final result by taking the total number of days in the loop -1.

#include <iostream>
using namespace std;

bool isLeapYear(int y){
	if(y % 4 == 0 && y % 100 != 0){
		return true;
	}else if(y % 400 == 0){
		return true;
	}
	return false;
}

int main(int argc, char** argv) {
	int y = 1777;
	int m = 4; 
	int d = 30;
	
	for(int i = 0; i < 8112; i++){
		d++;
		if(m == 12 && d == 32){
			y++;
			m = 1;
			d = 1;
			continue;
		} 
		if((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10) && d == 32){
			m++;
			d = 1;
			continue;
		}
		if((m == 4 || m == 6 || m == 9 || m == 11 ) && d == 31){
			m++;
			d = 1;
			continue;
		}
		if((m == 2) && isLeapYear(y) && d == 30){
			m ++;
			d = 1;
			continue;
		}
		if((m == 2) && !isLeapYear(y) && d == 29){
			m++;
			d = 1;
			continue;
		}
	}
	 
	cout << y << "-" << m << '-' << d;
	return 0;
}

From this question, what I learned is to use programming thinking to think and solve problems, to clarify what is to be done, how to do it, is the idea, as for how to do it, let the computer go.

The road ahead is long, come on!

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/115085669