catalogue
🤷♀️ first leap year determination
🤷♂️ second effectively handles Runping year
👍 third # day period comparison days
👻 Methods used this time
🤷♀️ first leap year determination
For date calculation, we should first introduce the judgment of leap year. There are two methods to judge leap year
bool is_leap(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); }
%400 is 0 or% 4 is 0% 100 is not 0
🤷♂️ second effectively handles Runping year
Every leap year will make the calculation difficult, so we can make the calculation concise in a way
bool is_leap(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } char * date(int day, int month, int year) { int sum= 0; if(month > 2 && is_leap(year)) sum += 1 + day; else sum += day; }
Judge the leap year, judge whether the month is greater than 2, and meet the number of days plus one.
👍 third # day period comparison days
It is troublesome to compare the number of days between two dates. If you compare directly, you must determine the leap year to see whether the year, month and day are the same. Here, we can take a better method on the premise that we know the minimum value of the comparison range
bool is_leap(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } int _sum(int year,int month,int day,int* days) { int sum=0; for(int i = 1971; i < year; i++) { if(is_leap(i)) sum += 366; else sum += 365; } for(int i = 1; i < month; i++) { sum += days[i]; } if(month > 2 && is_leap(year)) sum += 1 + day; else sum += day; return sum; }
Use_ sum function calculates the number of days between the two dates and 1971, and then subtracts them to obtain the absolute value
🐹 Problems and Solutions
🧐 Topic 1
To give you a date, please design an algorithm to determine which day of the week it corresponds to.
Enter three integers: day, month and year, representing day, month and year respectively.
The result you return must be one of these values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
Input: day = 31, month = 8, year = 2019 Output: "Saturday"
This problem needs to use an array to save the days of each month, and it's better to save the weeks. It's not so troublesome to write
bool is_leap(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } char * dayOfTheWeek(int day, int month, int year) { char* ans[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int sum= 0; for(int i = 1971; i < year; i++) { if(is_leap(i)) sum += 366; else sum += 365; } for(int i = 1; i < month; i++) { sum += days[i]; } if(month > 2 && is_leap(year)) sum += 1 + day; else sum += day; return ans[(5 + sum - 1) % 7]; }
😒 Topic 2
Give you a string , date, which represents a date in the format of , YYYY-MM-DD , Current chronology Date. Please calculate and return the day of the current year.
Generally, we think that January 1 is the first day of each year, January 2 is the second day of each year, and so on. The number of days in each month is consistent with the current era (Gregorian calendar)
Input: date = "2019-01-09" Output: 9
This problem needs to pay attention to changing characters into numbers and subtracting '0'
bool is_leap(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } int dayOfYear(char * date){ int year = (date[0] - '0') * 1000 + (date[1] - '0') * 100 + (date[2] - '0') * 10 + date[3] - '0'; int month = (date[5] - '0') * 10 + date[6] - '0'; int day = (date[8] - '0') * 10 + date[9] - '0'; int t=0; int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; for(int i=0;i<month-1;i++) { t+=days[i]; } if(month>2&&is_leap(year)) return t+day+1; return t+day; }
😶 Topic 3
Please write a program to calculate the number of days between two dates.
The date is given as a string in the format of {YYYY-MM-DD, as shown in the example.
Input: date1 = "2019-06-29", date2 = "2019-06-30" Output: 1
The method of this problem has been mentioned earlier, so I won't repeat it
bool is_leap(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } int _sum(int year,int month,int day,int* days) { int sum=0; for(int i = 1971; i < year; i++) { if(is_leap(i)) sum += 366; else sum += 365; } for(int i = 1; i < month; i++) { sum += days[i]; } if(month > 2 && is_leap(year)) sum += 1 + day; else sum += day; return sum; } int daysBetweenDates(char * date1, char * date2){ int year1 = (date1[0] - '0') * 1000 + (date1[1] - '0') * 100 + (date1[2] - '0') * 10 + date1[3] - '0'; int month1 = (date1[5] - '0') * 10 + date1[6] - '0'; int day1 = (date1[8] - '0') * 10 + date1[9] - '0'; int year2 = (date2[0] - '0') * 1000 + (date2[1] - '0') * 100 + (date2[2] - '0') * 10 + date2[3] - '0'; int month2 = (date2[5] - '0') * 10 + date2[6] - '0'; int day2 = (date2[8] - '0') * 10 + date2[9] - '0'; int sum1=0;int sum2=0; int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; sum1=_sum(year1,month1,day1,days); sum2=_sum(year2,month2,day2,days); if(sum2>sum1) return sum2-sum1; return sum1-sum2; }
The three algorithms have low time complexity and belong to the algorithm of space for time.