問題描述
中國有句俗語叫“三天打魚兩天曬網(wǎng)”。某人從1990年1月1日起開始“三天打魚兩天曬網(wǎng)”,問這個人在以后的某一天中是“打魚”還是“曬網(wǎng)”
問題分析
根據(jù)題意可以將解題過程分為三步:
(1)計算從1990年1月1日開始至指定日期共有多少天;
(2)由于“打魚”和“曬網(wǎng)”的周期為5天,所以將計算出的天數(shù)用5去除;
(3)根據(jù)余數(shù)判斷他是在“打魚”還是在“曬網(wǎng)”;若余數(shù)為1,2,3,則他是在“打魚”,否則 是在“曬網(wǎng)。
問題解決
import datetime
y = int (input ('請輸入4位數(shù)字的年份:')) # 獲取年份
m = int (input ('請輸入月份:')) # 獲取月份
d = int (input ('請輸入是哪一天:')) # 獲取“日”
targetDay = datetime.date (y, m, d) # 將輸入的日期格式化成標準的日期
dayCount = targetDay - datetime.date (1990, 1, 1) # 減去上一年最后一天
totalDay = dayCount.days+1
if(totalDay%5>0 and totalDay%5<4):
print("今天打魚")
else:
print("今天曬網(wǎng)")