三天打鱼两天晒⽹问题Python求解
三天打鱼两天晒⽹问题Python求解
⽬录
问题:
中国有句俗语叫“三天打鱼两天晒⽹”。某⼈从2010年1⽉1⽇起开始“三天打鱼两天晒⽹”,问这个⼈在以后的某⼀天中是“打鱼”还是“晒⽹”。
要求
基本要求:
1.程序风格良好,提供友好的输⼊输出。
提⾼要求:
1.输⼊数据的正确性验证。
2.使⽤⽂件进⾏数据测试。如将⽇期 20100101 20111214 等数据保存在in.txt⽂件中,程序读⼊in.dat⽂件进⾏判定,并将结果输出⾄⽂件。
作业思路:
根据题意可以将解题过程分为三步:
1)计算从2010年1⽉1⽇开始⾄指定⽇期共有多少天;
2)由于“打鱼”和“晒⽹”的周期为5天,所以将计算出的天数⽤5去除;
3)根据余数判断他是在“打鱼”还是在“晒⽹”;
功能实现
1.准备
导⼊calendar模块,便于计算⽇历相关数据
calendar.leapdays(y1,y2)
# 返回在Y1,Y2两年之间的闰年总数。
calendar.isleap(year)
# 返回True或False,判断是否为闰年。
其他详细说明点击
创建输⼊⽂件,保存在in.dat中,数据如下。
2.预先处理好相关数据
monthDayCommonYear =tuple([31,28,31,30,31,30,31,31,30,31,30,31])# the day of each common month monthDayLeapYear =tuple([31,29,31,30,31,30,31,31,30,31,30,31])# the day of each leap month commonYearDays =0
leapYearDays =0
while iFlag <12:
commonYearDays += monthDayCommonYear[iFlag]# the day of common year
leapYearDays += monthDayLeapYear[iFlag]# the day of leap year
iFlag +=1
3.读⼊dat⽂件
f =open(r"C:\Users\moree\Desktop\in.dat",'r')# the absolute file path
lines = f.readlines()
f.close()
4.判断输⼊数据是否合法:
分为年,⽉,⽇的判断
年份>2010,0<⽇期<(⼤⽉32||⼩⽉31),0<⽉份<13,0<⼆⽉<(闰年30||平年29);
if nyear <2010or nmonth >12or nmonth <1or nday <1:
print("illegal input!")
continue
if(nmonth ==1or nmonth ==3or nmonth ==5or nmonth ==7or nmonth ==8or nmonth ==10or nmonth)and nday >31:
print("illegal input!")
continue
if(nmonth ==4or nmonth ==6or nmonth ==9or nmonth ==11)and nday >30:
python怎么读取dat文件
print("illegal input!")
continue
isLeapYear = calendar.isleap(nyear)
if isLeapYear:# the leap year
if nmonth ==2and nday >29:
print("illegal input!")
continue
else:# the common year
if nmonth ==2and nday >28:
print("illegal input!")
continue
5.求出初始⽇期⾄⽬标⽇期的天数
先取出每个输⼊的年⽉⽇
# get single day, month and year
nday = ndate %100
nyear =int(ndate /10000)
nmonth =int(ndate /100)%100
使⽤之前提到的calendar模块与预先准备的leapYearDays等数据,计算出起始⽇期⾄⽬标⽇期的天数
isLeapYear = calendar.isleap(nyear)
if isLeapYear:# the leap year
leapYears = calendar.leapdays(2010, nyear)# get the leap years from 2020 to now
daySum =(nyear -2010- leapYears)* commonYearDays + leapYears * leapYearDays
while mFlag < nmonth:
daySum += monthDayLeapYear[mFlag]
mFlag +=1
mFlag =0
daySum += nday
else:# the common year
leapYears = calendar.leapdays(2010, nyear)# get the leap years from 2020 to now
daySum =(nyear -2010- leapYears)* commonYearDays + leapYears * leapYearDays
while mFlag < nmonth -1:
daySum += monthDayCommonYear[mFlag]
mFlag +=1
mFlag =0
daySum += nday
总天数对5取模,计算具体⽇期并输出到
fSave =open(r"C:\Users\moree\",'a')# save results
ins = daySum %5
if ins !=0and ins <=3:
fSave.write(str(ndate)+":打渔!\n")
else:
fSave.write(str(ndate)+":晒⽹!\n")
功能完成,因为是循环读取dat⽂件,所以要将所有代码放⼊循环之中,获取⽂件长度,确定循环计数器,从⽽完成⽂件的循环读写。
line =len(lines)
while iFlag < line:
### add your code here
fSave.close()
最后,莫忘记关闭⽂件
6.输出结果
完整代码如下
import calendar
iFlag =0
mFlag =0
daySum =0
monthDayCommonYear =tuple([31,28,31,30,31,30,31,31,30,31,30,31])# the day of each common month monthDayLeapYear =tuple([31,29,31,30,31,30,31,31,30,31,30,31])# the day of each leap month commonYearDays =0
leapYearDays =0
while iFlag <12:
commonYearDays += monthDayCommonYear[iFlag]# the day of common year
leapYearDays += monthDayLeapYear[iFlag]# the day of leap year
iFlag +=1
iFlag =0# initialize the Flag
# read the text file named "DATE_TXT" at desktop, then save all lines
f =open(r"C:\Users\moree\Desktop\in.dat",'r')# the absolute file path
lines = f.readlines()
f.close()
# get the length of txt file
line =len(lines)
while iFlag < line:
ndate =int(lines[iFlag-1].replace("\n",""))# delete extending whitespaces
iFlag +=1# the flag step 1
# get single day, month and year
nday = ndate %100
nyear =int(ndate /10000)
nyear =int(ndate /10000)
nmonth =int(ndate /100)%100
if nyear <2010or nmonth >12or nmonth <1or nday <1:
print("illegal input!")
continue
if(nmonth ==1or nmonth ==3or nmonth ==5or nmonth ==7or nmonth ==8or nmonth ==10or nmonth)and nday >31: print("illegal input!")
continue
if(nmonth ==4or nmonth ==6or nmonth ==9or nmonth ==11)and nday >30:
print("illegal input!")
continue
isLeapYear = calendar.isleap(nyear)
if isLeapYear:# the leap year
if nmonth ==2and nday >29:
print("illegal input!")
continue
leapYears = calendar.leapdays(2010, nyear)# get the leap years from 2020 to now
daySum =(nyear -2010- leapYears)* commonYearDays + leapYears * leapYearDays
while mFlag < nmonth:
daySum += monthDayLeapYear[mFlag]
mFlag +=1
mFlag =0
daySum += nday
else:# the common year
if nmonth ==2and nday >28:
print("illegal input!")
continue
leapYears = calendar.leapdays(2010, nyear)# get the leap years from 2020 to now
daySum =(nyear -2010- leapYears)* commonYearDays + leapYears * leapYearDays
while mFlag < nmonth -1:
daySum += monthDayCommonYear[mFlag]
mFlag +=1
mFlag =0
daySum += nday
# print("daySum:" + str(daySum))
fSave =open(r"C:\Users\moree\",'a')# save results
ins = daySum %5
if ins !=0and ins <=3:
fSave.write(str(ndate)+":打渔!\n")
else:
fSave.write(str(ndate)+":晒⽹!\n")
daySum =0
fSave.close()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。