老师参与

040:万年历 一题苦思不得其解,向老师和同学们求助!

周文扬 发表于2021年02月16日
<p><strong>此题经反复调试程序,一直不通过。但逐一检查程序的输入输出又是符合的,不知道问题到底出在哪里。求助!</strong></p><p>040:万年历</p><p>描述<br ></p><p>给定年月日,求星期几。已知2020年11月18日是星期三。另外,本题有公元0年,这个和真实的纪年不一样</p><p>输入</p><p>第一行是n(n &lt;=30),表示有n组数据<br >接下来n行,每行是一组数据。<br >每行三个整数y,m,d,分别代表年,月,日。(-1000000&lt;=y&lt;=1000000)<br >若今年是2017年,则往前就是2016年,2015年....一直数到2年,1年,再往前就是0年,-1年,-2年.....</p><p>输出</p><p>对每组数据,输出星期几,星期几分别用<br >&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;, &quot;Friday&quot;,&quot;Saturday&quot; 表示<p><br ></p>如果月份和日期不合法,输出&quot;Illegal&quot;</p><p><code class="brush:python;toolbar:false" >import&nbsp;calendar&nbsp;&nbsp;#导入Python的日历标准库 n=int(input()) lb=[input().split()&nbsp;for&nbsp;i&nbsp;in&nbsp;range(n)] xqlb=[&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&nbsp;&quot;Friday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot;] #此函数判断输入是否合法 def&nbsp;pdhf(lb): #&nbsp;print(lb[0],lb[1],lb[2]) if&nbsp;calendar.isleap(int(lb[0])): if&nbsp;lb[1]==&quot;2&quot;&nbsp;and&nbsp;int(lb[2])&gt;29: return&nbsp;0 else: if&nbsp;lb[1]==&quot;2&quot;&nbsp;and&nbsp;int(lb[2])&gt;28: return&nbsp;0 if&nbsp;int(lb[1])&gt;12: return&nbsp;0 if&nbsp;lb[1]&nbsp;in&nbsp;[&quot;4&quot;,&quot;6&quot;,&quot;9&quot;,&quot;11&quot;]&nbsp;and&nbsp;int(lb[2])&gt;30: return&nbsp;0 if&nbsp;lb[1]&nbsp;in&nbsp;[&quot;1&quot;,&quot;3&quot;,&quot;5&quot;,&quot;7&quot;,&quot;8&quot;,&quot;10&quot;,&quot;12&quot;]&nbsp;and&nbsp;int(lb[2])&gt;31: return&nbsp;0 return&nbsp;1 for&nbsp;i&nbsp;in&nbsp;lb: if&nbsp;pdhf(i): rq&nbsp;=&nbsp;calendar.weekday(int(i[0]),int(i[1]),int(i[2])&nbsp;)&nbsp; print(xqlb[rq]) else: print(&quot;Illegal&quot;)</code></p>
7 回复

    1楼

  • 周文扬 发表于2021年02月16日
    2 | 0 | 举报
    <p><code class="brush:python;toolbar:false" >#此题其实有个取巧的办法,就是用calendar库遇到不符合日期抛出异常来省略人为判断日期格式,输出结果和上面程序结果是一致的。但OJ上同样是报错的。这程序是报Wrong&nbsp;Answer,主文中的是报Runtime&nbsp;Error import&nbsp;calendar n=int(input()) lb=[input().split()&nbsp;for&nbsp;i&nbsp;in&nbsp;range(n)] xqlb=[&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&nbsp;&quot;Friday&quot;,&quot;Saturday&quot;,&quot;Sunday&quot;] for&nbsp;i&nbsp;in&nbsp;lb: try: rq&nbsp;=&nbsp;calendar.weekday(int(i[0]),int(i[1]),int(i[2])&nbsp;)&nbsp; except: print(&quot;Illegal&quot;) else: print(xqlb[rq])</code></p>
    周文扬 发表于2021年02月16日
    添加评论
  • 2楼

  • 北大郭炜 发表于2021年02月16日
    2 | 0 | 举报
    <p>问题出在程序输入输出不符合呗。都说有了有公元0年,和真实纪年不一样了,怎么可能用calendar做,calendar没有公元0年</p>
    北大郭炜 发表于2021年02月16日
    添加评论
  • 3楼

  • 周文扬 发表于2021年02月16日
    0 | 0 | 举报
    <p>郭老师,经测试calendar能对公元0年进行判断,如下图:</p><p><img src="https://nos.netease.com/edu-image/6da9709bb63c4766bd7c8816ad644862.png" /></p>
    周文扬 发表于2021年02月16日
    添加评论
  • 4楼

  • 周文扬 发表于2021年02月16日
    0 | 0 | 举报
    <p>有点奇怪,datetime库不能对公元0年判断,但calendar库可以</p>
    周文扬 发表于2021年02月16日
    添加评论
  • 5楼

  • 慕小猪 发表于2021年02月16日
    0 | 0 | 举报
    <p>你可以自己写个calendar,我就是自己写的……</p>
    慕小猪 发表于2021年02月16日
    添加评论
  • 6楼

  • 周文扬 发表于2021年02月17日
    0 | 0 | 举报
    <p>谢谢慕小猪同学提醒。问题已解决,尝试八次,方才通过。哈哈</p><p>问题可能出在OJ系统不识别calendar库,因此用datetime库重写此程序,解决年份超出范围这一问题我是对不合法的年份用加减6000年倍数的方法使其变成合理范围的。</p><p><img src="https://nos.netease.com/edu-image/a271c86e076a4ab68d112e801e2b0633.png" /></p><p><br /></p><p><br /></p>
    周文扬 发表于2021年02月17日
    添加评论
  • 7楼

  • 符义 发表于2021年03月20日
    0 | 0 | 举报
    <p>我觉得老师并不是想要我们以这种方法来解决该题……</p>
    符义 发表于2021年03月20日
    添加评论