三天打魚,兩天曬網(Python語言)

問題描述:

某人從2010年1月1日起開始“三天打魚兩天曬網”,問這個人在以後的某一天中是“打魚”還是“曬網”。

解題思路:

通過給定的時間結點判斷從指定日期2010年1月1日計算出時間差。每一個打魚曬網週期爲5天,用計算出來的時間差除以每一個週期所得的餘數即可判定當日到底是打魚還是曬網。

解題步驟:

  1. 通過輸入或者讀取文件中的字符串類型指定日期(String),將字符串進行適當處理(轉變爲int類型)將年、月、日分別提取出來

  2. 根據年份判斷該年是否爲閏年,進而判斷每個月所對應的天數是否正確(小月天數爲1-30,大月天數爲1-31,閏年2月爲29天,平年2月爲28天)

  3. 計算從當前日期到2010年1月1日相差的天數

  4. 總天數處以5所得的餘數判斷當天是在打魚還是曬網(餘數爲1,2,3爲打魚,餘數爲0,4爲曬網)

  5. 根據以上結果得出結論,並將結果保存到文件中

詳細代碼:

判斷該年是否爲閏年

#判斷是否爲閏年
def isBissextile(year):
    if (year%4==0 and year%100!=0) or year%400==0:
        return True
    else:
        return False

檢查日期是否正確

#檢查日期是否合理,否則結束程序
def checkDate(year,month,day):
    if year<2010:
        print("%d-%d-%d日期日期不得小於2010年"%(year,month,day))
        quit()
##首先判斷是否爲閏年,並作標記用於檢驗2月分日期
    if isBissextile(year):
        leapyear=True
    else:
        leapyear=False
##其次檢查輸入的月份和日期是否在合理範圍
    if (month in range(1,13)) or (day in range(1,32)):
##最後再檢查每個月份的天數是否合理
        if month in(1,3,5,7,8,10,12) :
            if day<1 or day>31:
                print("%d-%d-%d日期有誤,請檢查後重試"%(year,month,day))
                exit(0)
        else:
            if day<1 or day>30:
                print("%d-%d-%d日期有誤,請檢查後重試"%(year,month,day))
                exit(0)  
    else:
         print("%d-%d-%d日期有誤,請檢查後重試"%(year,month,day))
         exit()

    if month==2:
        if leapyear==False :
            if day<1 or day>28:
                print("%d-%d-%d日期有誤,請檢查後重試"%(year,month,day))
                exit(0)
        else:
             if day<1 or day>29:
                print("%d-%d-%d日期有誤,請檢查後重試"%(year,month,day))
                exit(0)

計算指定日期到2010-1-1的天數

#計算從2010-1-1到指定日期的天數,並返回總天數
def getDays(year,month,day):
    total_days=0
##判斷該年是否爲閏年,並加上相應的天數
    for count in range(2010,year):
        if isBissextile(count):
            total_days+=366
        else:
            total_days+=365
    days1=(31,28,31,30,31,30,31,31,30,31,30,31)#平年各月天數
    days2=(31,29,31,30,31,30,31,31,30,31,30,31)#閏年各月天數
#判斷如年後加上相應月數的天數
    if isBissextile(year):
        for each in range(0,month-1):
            total_days+=days2[each]
    else:
        for each in range(0,month-1):
            total_days+=days1[each]
#加上天數
    total_days+=day
    return total_days

判斷當天具體的事情

#判斷指定日期是在打魚還是曬網,並返回字符串結果
def isDoing(year,month,day):
    checkDate(year,month,day)
    days=getDays(year,month,day)
    last=days%5
    print("2010年1月1日距%d年%d月%d日相隔%d天"%(year,month,day,days))
    print("餘數爲%d:"%(last),end="")
    if last in (1,2,3):
        print("該天打魚\n")
        return str(year)+"-"+str(month)+"-"+str(day)+"打魚\n"
    else:
        print("該天曬網\n")
        return str(year)+"-"+str(month)+"-"+str(day)+"曬網\n"

從文件中得到日期(進一步要求)

對文件的讀寫操作

Python中文件的讀取和寫入https://blog.csdn.net/sinat_34474705/article/details/77389258

從文件中讀入日期

##從文件中讀入日期,並返回日期數組
def fileRead():
    f=open("date.txt","r")#以只讀方式讀取文件
    context=[]
    while True:##將文件讀取結果放入到context數組中
        each=f.readline()
        if each :
            context.append(each.replace("\n",""))
        else:
            break
    f.close()
    print("讀取文件結果:")
    for each in context:
        print(each)
    print()
    return context

對日期進行處理,提取年月日

##對日期進行處理,提取其中的年月日
import re
def getDate(context):
    year=[]
    month=[]
    day=[]
    for str_date in context: 
        result1=re.search(r"\d{4}-",str_date)#使用正則表達式提取字符串日期中的年份
        result2=re.search(r"-\d{1,2}-",str_date)#使用正則表達式提取字符串日期中的月份
        result3=re.search(r"-\d{1,2}$",str_date)#使用正則表達式提取字符串日期中的日期
        try:
        ##去除年、月、日中的其他字符,並轉換爲int類型 
            year.append(int(result1.group(0).replace("-","")))
            month.append(int(result2.group(0).replace("-","")))
            day.append(int(result3.group(0).replace("-","")))
        ##    date=datetime.datetime.
        except AttributeError:
            print("%d-%d-%d日期有誤,請檢查後重試"%(year,month,day))
    date=[year,month,day]
    return date

運行結果

 

讀取文件結果:
2010-2-28
2014-5-30
2016-6-21
2018-8-29

2010年1月1日距2010年2月28日相隔59天
餘數爲4:該天曬網

2010年1月1日距2014年5月30日相隔1611天
餘數爲1:該天打魚

2010年1月1日距2016年6月21日相隔2364天
餘數爲4:該天曬網

2010年1月1日距2018年8月29日相隔3163天
餘數爲3:該天打魚

源代碼下載:https://download.csdn.net/download/xust_kevin/10633988

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章