首页 > 试题广场 >

Day of Week

[编程题]Day of Week
  • 热度指数:22969 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2005, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入描述:
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.


输出描述:
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
示例1

输入

9 October 2001
14 October 2001

输出

Tuesday
Sunday
import datetime
print datetime.datetime.strptime(raw_input(), "%d %B %Y").strftime("%A")

Python其实只要两行233

编辑于 2018-02-08 16:37:22 回复(2)

Python四行代码,利用datetime库。

import datetime
while True:
    try:
        month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
                 'November', 'December']
        week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
        d, m, y = input().split()
        print(week[int(datetime.datetime(int(y), month.index(m) + 1, int(d)).strftime("%w"))])
    except:
        break

看了下面的答案后,发现还可以更简单,"A"直接输出的是Tuesday这种格式,太方便了。于是代码可以缩成三行:

import datetime
while True:
    try:
        month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December']
        d, m, y = input().split()
        print((datetime.datetime(int(y), month.index(m) + 1, int(d)).strftime("%A")))
    except:
        break

人生苦短,我爱python。

编辑于 2019-04-18 20:51:19 回复(2)
from datetime import datetime
Month = {'May': 5, 'December': 12, 'October': 10, 'August': 8,
         'July': 7, 'February': 2, 'January': 1, 'September': 9,
         'March': 3, 'April': 4, 'November': 11, 'June': 6}
try:
    while 1:
        k = raw_input().split()
        print datetime(int(k[2]),Month[k[1]],int(k[0])).strftime('%A').capitalize()
except:
    pass

发表于 2016-12-25 16:52:08 回复(0)

问题信息

难度:
3条回答 11186浏览

热门推荐

通过挑战的用户

查看代码