三天打鱼两天晒网问题Python求解
三天打鱼两天晒网问题Python求解
目录
使用Python求解三天打鱼两天晒网问题。
使用Java模拟XN*2图灵机
问题:
中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
要求
基本要求:
1.程序风格良好,提供友好的输入输出。
提高要求:
1.输入数据的正确性验证。
2.使用文件进行数据测试。如将日期 20100101 20111214 等数据保存在in.txt文件中,程序读入in.dat文件进行判定,并将结果输出至out.txt文件。
作业思路:
根据题意可以将解题过程分为三步:
1)计算从2010年1月1日开始至指定日期共有多少天;
2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除;
3)根据余数判断他是在“打鱼”还是在“晒网”;
功能实现
1.准备
导入calendar模块,便于计算日历相关数据
calendar.leapdays(y1,y2)
# 返回在Y1,Y2两年之间的闰年总数。calendar.isleap(year)
# 返回True或False,判断是否为闰年。
其他详细说明点击python日历模块calendar
创建输入文件,保存在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 yearleapYearDays += monthDayLeapYear[iFlag] # the day of leap yeariFlag += 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 < 2010 or nmonth > 12 or nmonth < 1 or nday < 1:print("illegal input!")continueif (nmonth == 1 or nmonth == 3 or nmonth == 5 or nmonth == 7 or nmonth == 8 or nmonth == 10 or nmonth) and nday > 31:print("illegal input!")continueif (nmonth == 4 or nmonth == 6 or nmonth == 9 or nmonth == 11) and nday > 30:print("illegal input!")continueisLeapYear = calendar.isleap(nyear)if isLeapYear: # the leap yearif nmonth == 2 and nday > 29:print("illegal input!")continueelse: # the common yearif nmonth == 2 and nday > 28:print("illegal input!")continue
5.求出初始日期至目标日期的天数
先取出每个输入的年月日
# get single day, month and yearnday = ndate % 100nyear = int(ndate / 10000)nmonth = int(ndate / 100) % 100
使用之前提到的calendar模块与预先准备的leapYearDays等数据,计算出起始日期至目标日期的天数
isLeapYear = calendar.isleap(nyear)if isLeapYear: # the leap yearleapYears = calendar.leapdays(2010, nyear) # get the leap years from 2020 to nowdaySum = (nyear - 2010 - leapYears) * commonYearDays + leapYears * leapYearDayswhile mFlag < nmonth:daySum += monthDayLeapYear[mFlag]mFlag += 1mFlag = 0daySum += ndayelse: # the common yearleapYears = calendar.leapdays(2010, nyear) # get the leap years from 2020 to nowdaySum = (nyear - 2010 - leapYears) * commonYearDays + leapYears * leapYearDayswhile mFlag < nmonth - 1:daySum += monthDayCommonYear[mFlag]mFlag += 1mFlag = 0daySum += nday
总天数对5取模,计算具体日期并输出到out.txt
fSave = open(r"C:\Users\moree\Desktop\out.txt", 'a') # save resultsins = daySum % 5if ins != 0 and 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 calendariFlag = 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 yearleapYearDays += monthDayLeapYear[iFlag] # the day of leap yeariFlag += 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 whitespacesiFlag += 1 # the flag step 1# get single day, month and yearnday = ndate % 100nyear = int(ndate / 10000)nmonth = int(ndate / 100) % 100if nyear < 2010 or nmonth > 12 or nmonth < 1 or nday < 1:print("illegal input!")continueif (nmonth == 1 or nmonth == 3 or nmonth == 5 or nmonth == 7 or nmonth == 8 or nmonth == 10 or nmonth) and nday > 31:print("illegal input!")continueif (nmonth == 4 or nmonth == 6 or nmonth == 9 or nmonth == 11) and nday > 30:print("illegal input!")continueisLeapYear = calendar.isleap(nyear)if isLeapYear: # the leap yearif nmonth == 2 and nday > 29:print("illegal input!")continueleapYears = calendar.leapdays(2010, nyear) # get the leap years from 2020 to nowdaySum = (nyear - 2010 - leapYears) * commonYearDays + leapYears * leapYearDayswhile mFlag < nmonth:daySum += monthDayLeapYear[mFlag]mFlag += 1mFlag = 0daySum += ndayelse: # the common yearif nmonth == 2 and nday > 28:print("illegal input!")continueleapYears = calendar.leapdays(2010, nyear) # get the leap years from 2020 to nowdaySum = (nyear - 2010 - leapYears) * commonYearDays + leapYears * leapYearDayswhile mFlag < nmonth - 1:daySum += monthDayCommonYear[mFlag]mFlag += 1mFlag = 0daySum += nday# print("daySum:" + str(daySum))fSave = open(r"C:\Users\moree\Desktop\out.txt", 'a') # save resultsins = daySum % 5if ins != 0 and ins <= 3:fSave.write(str(ndate) + ":打渔!\n")else:fSave.write(str(ndate) + ":晒网!\n")daySum = 0
fSave.close()
三天打鱼两天晒网问题Python求解相关推荐
- python天天向上的力量三天打鱼两天晒网_017 示例3-天天向上的力量-Go语言中文社区...
一."天天向上的力量"问题分析 1.1 天天向上的力量 基本问题:持续的价值 一年365天,每天进步1%,累计进步多少呢? (1.01^{365}) 一年365天,每天退步1%,累 ...
- python天天向上的力量三天打鱼两天晒网_天天向上的力量
我前阵子三天打鱼两天晒网地学了一点Python,也就开个头的样子吧,然后就不想学了,一鼓作气再而衰三而竭了.我学到课程的第三个例子,老师给起的名字叫"天天向上的力量". 其实就是那 ...
- Python : 7-6 三天打鱼两天晒网 (15 分)
Python : 7-6 三天打鱼两天晒网 (15 分) 中国有句俗语叫"三天打鱼两天晒网".假设某人从某天起,开始"三天打鱼两天晒网",问这个人在以后的第N天 ...
- 中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”, 问这个人在以后的某一天中是“打鱼”还是“晒网”。用C或C++语言/java/python实现程序解决问题
/* 中国有句俗语叫"三天打鱼两天晒网".某人从2010年1月1日起开始"三天打鱼两天晒网", 问这个人在以后的某一天中是"打鱼"还是&qu ...
- 三天打鱼两天晒网(python)
三天打鱼两天晒网 问题描述:中国有句俗语叫"三天打鱼两天晒网".某人从2010年1月1日起开始"三天打鱼两天晒网",问这个人在以后的某一天中是"打鱼& ...
- python天天向上的力量三天打鱼两天晒网_三天打鱼两天晒网问题
题目:中国有句俗语叫"三天打鱼两天晒网".某人从2010年1月1日起开始"三天打鱼两天晒网",问这个人在以后的某一天中是"打鱼"还是&quo ...
- Python之三天打鱼两天晒网
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.pandas是什么? 二.使用步骤 1.引入库 2.读入数据 总结 前言 今天老师在课堂上教我们如何运用Pytho ...
- 三天打鱼,两天晒网(python)
三天打鱼两天晒网(python) 问题概述:中国有句话叫"三天打鱼两天晒网",一个人从2021年5月15日起开始"三天打鱼两天晒网",那么这个人在以后的某一天中 ...
- 三天打鱼两天晒网python程序设计_Python经常会遇到三天的笔试题:钓鱼和两天的晒网:,之,三天打鱼,两天晒网...
问题概述:中国有句俗语叫"三天打鱼两天晒网"某人从1990年1月1日起开始"三天打鱼两天晒网",问这个人在以后的某一天中是"打鱼"还是&qu ...
最新文章
- too many levels of symbolic links的错误
- python yield与递归
- 降低 80% 的读写响应延迟!我们测评了 etcd 3.4 新特性(内含读写发展史)
- Qt::Key_Return和Qt::Key_Enter区别
- 【数论】Concatenated Multiples【codeforces-Round #506-div3-D】
- Kafka面试题总结
- php探针作用,php探针使用原理和技巧讲解
- 一般线性模型和混合线性模型_从零开始的线性混合模型
- Python+Tushare股票数据分析
- 素数筛(c语言实现)
- 微信小程序——搭建自己的服务器
- 谷歌浏览器翻译失效解决方案,百试百灵
- 豌豆淘谈护肤:豌豆淘讲解肤质分类,不了解肤质谈何护肤?
- 月薪10k,20k,30k,40k及以上的程序员分别有何不同?
- 新未来,决战支付大数据
- C语言中的结构体(struct)详解
- CentOS上安装运行XWiKi
- 【软件测试】测试用例的设计
- 全面质量管理的常用长种工具
- SEO工作时都会用哪些辅助工具