✨ preface ✨
In this series, bloggers are ready to share the algorithm of punching in and learning in the community of 10000 people and 1000 questions every day. Bloggers are also Xiaobai, so they can understand the confusion of novices when brushing questions, so pay attention to bloggers and learn an algorithm every day. At the same time, you are also welcome to join the activity of learning thousands of questions for 10000 people. It is the so-called: a person can walk fast, but a group of people can go further.
catalogue
1, Algorithm thought notes
① Leap year calculation
What is a leap year? The number divisible by 400 is a leap year, and the number divisible by 4 but not divisible by 100 is also a leap year, so we can write a function to judge whether it is a leap year
int is_leap(int year) { if(year % 400 == 0 || year %4 == 0 && year % 100 != 0) return 1; else return 0; }
② Joseph Ring thought
Suppose today is Friday, how to write code to judge the day of the week in 10 days?
The first code you might think of is this
(5+10)% 7
Pinch your fingers, eh, that's really right. But the disadvantage of the above thought lies in (6 + 1)% 7 Can't judge. So we thought, if only the week started from week 0, there would be no problem. You're right. Yes, we just need to start week from "week 0"
(day - 1 + △day)% 7 +1
We artificially start the week from 0, as long as we add back the day we lost
2, What day of the week
① Problem presentation
② Code drill
int is_leap(int year) { if(year % 400 == 0 || year %4 == 0 && year % 100 != 0) return 1; else return 0; } char * dayOfTheWeek(int day, int month, int year) { int i = 0; int daysum = 0; int dayOfmonth[]={ 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; char* ch[] = { "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday" }; for( i = 1971;i < year; i++) { daysum += is_leap( i ) ? 366 : 365; } for(i=1;i<month;i++) { daysum+=dayOfmonth[i]; } daysum+=day; if(month > 2 && is_leap(year)) { daysum++; } return ch[(3+daysum)%7];//The embodiment of Joseph's ring thought }
3, 1154. What day of the year
1154. What day of the yearhttps://leetcode-cn.com/problems/day-of-the-year/
① Topic presentation
② Code drill
int is_leap(int year)//Determine whether it is a leap year { if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) return 1; else return 0; } int strToint(char* date, int size)//String to number function { int sum = 0; for (int i = 0; i < size; i++) { sum = sum * 10 + date[i] - '0';//Pay attention to this' 0 ', don't forget, think about why? } return sum; } int dayOfYear(char* date) { int sumday = 0; int year = strToint(date + 0, 4); int month = strToint(date + 5, 2); int day = strToint(date + 8, 2); int monthday[] = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30, 31 }; for (int i = 1; i < month; i++) { sumday += monthday[i]; } sumday += day; if (month > 2 && is_leap(year)) { sumday++; } return sumday; }
4, Homework after class
(friendly tips, the combination of the above two questions)
1360. Days between dateshttps://leetcode-cn.com/problems/number-of-days-between-two-dates/