📜  查找一年中每天发生的次数

📅  最后修改于: 2022-05-13 01:55:21.872000             🧑  作者: Mango

查找一年中每天发生的次数

给定一年。您的任务是找出一年中每一天的数量,即该给定年份的星期一、星期二、星期三、星期四、星期五、星期六、星期日的数量。
例子:

Input: 2019
Output: Monday-52
        Tuesday-53
        Wednesday-52
        Thursday-52
        Friday-52
        Saturday-52
        Sunday-52

Input: 2024
Output: Monday-53
        Tuesday-53
        Wednesday-52
        Thursday-52
        Friday-52
        Saturday-52
        Sunday-52

观察:我们必须做一些关键的观察。第一个是一年至少有 52 周,所以每天至少会发生 52 次。由于 52*7 是 364,所以任何一年的 1 月 1 日这一天都会出现 53 次,如果该年是闰年,那么 1 月 2 日的这一天也会出现 53 次。
方法:创建一个大小为 7 的列表,初始值为 52,因为最小出现次数为 52。找到第一天的索引。计算出现的天数为 53。
下面是实现。

Python3
# python program Find number of
# times every day occurs in a Year
  
 
import datetime
import calendar
  
def day_occur_time(year):
     
    # stores days in a week
    days = [ "Monday", "Tuesday", "Wednesday", 
           "Thursday",  "Friday", "Saturday",
           "Sunday" ]
     
    # Initialize all counts as 52
    L = [52 for i in range(7)]
     
    # Find the index of the first day
    # of the year
    pos = -1
    day = datetime.datetime(year, month = 1, day = 1).strftime("%A")
    for i in range(7):
        if day == days[i]:
            pos = i
             
    # mark the occurrence to be 53 of 1st day
    # and 2nd day if the year is leap year
    if calendar.isleap(year):
        L[pos] += 1
        L[(pos+1)%7] += 1
         
    else:
        L[pos] += 1
         
     
    # Print the days
    for i in range(7):
        print(days[i], L[i])
      
  
# Driver Code
year = 2019
day_occur_time(year)


输出:

Monday 52
Tuesday 52
Wednesday 52
Thursday 52
Friday 53
Saturday 53
Sunday 52