如何用c语言计算所有存在黑色星期五的月份?黑色星期五怎么用C语言判定
878
2022-08-22
[leetcode] 1185. Day of the Week
Description
Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the day, month and year respectively.
Return the answer as one of the following values {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}.
Example 1:
Input: day = 31, month = 8, year = 2019Output: "Saturday"
Example 2:
Input: day = 18, month = 7, year = 1999Output: "Sunday"
Example 3:
Input: day = 15, month = 8, year = 1993Output: "Sunday"
Constraints:
The given dates are valid dates between the years 1971 and 2100.
分析
题目的意思是:给定年月日,要算出是星期几,这道题本身可以调用python的一些包就可以解决,但是我感觉不是出这道题的重点,所以转向了通过计算判断事周几,首先要搞清楚闰年有几年,然后month有几天,day有几天,然后除以7取余数就行了。
代码
class Solution: def dayOfTheWeek(self, day: int, month: int, year: int) -> str: prev_year=year-1 days=prev_year*365+prev_year//4-prev_year//100+prev_year//400 days+=sum([31,28,31,30,31,30,31,31,30,31,30,31][:month-1]) days+=day if(month>2 and ((year%4==0 and year%100!=0) or (year%400==0))): days+=1 return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][days % 7]
参考文献
[LeetCode] Simple & Easy Solution by Python 3
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。