星期几——蓝桥杯(Python解法)
已知2011年11月11日是星期五,问YYYY年MM月DD日是星期几?
注意考虑闰年的情况。尤其是逢百年不闰,逢400年闰的情况。
输入格式
输入只有一行
YYYY MM DD
输出格式
输出只有一行
W
数据规模和约定
1599 <= YYYY <= 2999
1 <= MM <= 12
1 <= DD <= 31,且确保测试样例中YYYY年MM月DD日是一个合理日期
1 <= W <= 7,分别代表周一到周日
样例输入
2011 11 11
样例输出
5
Python示例(输入必须为真实存在且和为题目要求相一致的输入,例如1111年11月11日和2011年2月30日就不能作为输入,类似边界条件待补充)
a = "2011 11 11"
def ifRunNian(y1):
y = int(y1)
if y >= 1599 and y <= 2999:
if y%4==0 :
if y%100==0:
if y%400==0:
return 366
else:
return 365
else:
return 366
else:
return 365
else:
return -1
def month2day(y1, m1):
m = int(m1)
yb = ifRunNian(y1)
if m <= 12 and m >= 1:
if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:
return 31
elif m == 4 or m == 6 or m == 9 or m == 11:
return 30
elif m == 2:
if yb == 366:
return 29
elif yb == 365:
return 28
print(m)
def getDeltaYear(y1):
y = int(y1)
deltaYList = []
if y < 2011 and y <= 2999:
for i in range(2011-y):
deltaYList.append(y+i)
elif y>=2011 and y <= 2999:
for i in range(y-2011):
deltaYList.append(2011+i)
return tuple(deltaYList)
def getDeltaDay(y1, m1, d1):
deltaAllDay = 0
y = int(y1)
m = int(m1)
d = int(d1)
deltaMList = []
dy = getDeltaYear(y1)
if y < 2011 or (y == 2011 and m < 11) or (y == 2011 and m == 11 and d < 11):
if dy != ():
for i in dy:
if i == dy[0]:
for j in range(m, 13):
# print(int(j))
md = month2day(i, j)
deltaAllDay += md
deltaAllDay -= d
else:
deltaAllDay += ifRunNian(i)
for j in range(1, 11):
deltaAllDay += month2day(int(i)+1, j)
deltaAllDay += 11
else:
i = 2011
for j in range(m, 11):
md = month2day(i, j)
deltaAllDay += md
deltaAllDay -= d
deltaAllDay += 11
deltaAllDay = -deltaAllDay
# elif y < 2011 or (y == 2011 and m < 11) or (y == 2011 and m == 11 and d < 11):
elif y > 2011 or (y == 2011 and m > 11) or (y == 2011 and m == 11 and d > 11):
if dy != ():
print(dy)
for i in dy:
if i == dy[0]:
for j in range(11, 13):
deltaAllDay += month2day(i, j)
deltaAllDay -= 11
else:
deltaAllDay += ifRunNian(i)
for j in range(1, m):
deltaAllDay += month2day(int(i)+1, j)
deltaAllDay += d
else:
i = 2011
for j in range(11, m):
md = month2day(i, j)
deltaAllDay += md
deltaAllDay -= 11
deltaAllDay += d
return deltaAllDay
def day2week(deltaAllDay):
if deltaAllDay < 0:
deltaAllDay = - deltaAllDay
e = 5- deltaAllDay % 7
print(e)
while e <= 0:
e += 7
while e > 7:
e -= 7
print(f'星期{e}')
else:
e = 5 + deltaAllDay % 7
while e <= 0:
e += 7
while e > 7:
e -= 7
print(f'星期{e}')
print(a)
ymd = a.split(" ")
y, m, d = (ymd[0], ymd[1], ymd[2])
print(f'{y}, {m}, {d}')
# day2week(y, m, d)
k = getDeltaDay(1949, 10, 1)
print(k)
day2week(k)
print(month2day(2012, 2))
注意考虑闰年的情况。尤其是逢百年不闰,逢400年闰的情况。
输入格式
输入只有一行
YYYY MM DD
输出格式
输出只有一行
W
数据规模和约定
1599 <= YYYY <= 2999
1 <= MM <= 12
1 <= DD <= 31,且确保测试样例中YYYY年MM月DD日是一个合理日期
1 <= W <= 7,分别代表周一到周日
样例输入
2011 11 11
样例输出
5
Python示例(输入必须为真实存在且和为题目要求相一致的输入,例如1111年11月11日和2011年2月30日就不能作为输入,类似边界条件待补充)
a = "2011 11 11"
def ifRunNian(y1):
y = int(y1)
if y >= 1599 and y <= 2999:
if y%4==0 :
if y%100==0:
if y%400==0:
return 366
else:
return 365
else:
return 366
else:
return 365
else:
return -1
def month2day(y1, m1):
m = int(m1)
yb = ifRunNian(y1)
if m <= 12 and m >= 1:
if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:
return 31
elif m == 4 or m == 6 or m == 9 or m == 11:
return 30
elif m == 2:
if yb == 366:
return 29
elif yb == 365:
return 28
print(m)
def getDeltaYear(y1):
y = int(y1)
deltaYList = []
if y < 2011 and y <= 2999:
for i in range(2011-y):
deltaYList.append(y+i)
elif y>=2011 and y <= 2999:
for i in range(y-2011):
deltaYList.append(2011+i)
return tuple(deltaYList)
def getDeltaDay(y1, m1, d1):
deltaAllDay = 0
y = int(y1)
m = int(m1)
d = int(d1)
deltaMList = []
dy = getDeltaYear(y1)
if y < 2011 or (y == 2011 and m < 11) or (y == 2011 and m == 11 and d < 11):
if dy != ():
for i in dy:
if i == dy[0]:
for j in range(m, 13):
# print(int(j))
md = month2day(i, j)
deltaAllDay += md
deltaAllDay -= d
else:
deltaAllDay += ifRunNian(i)
for j in range(1, 11):
deltaAllDay += month2day(int(i)+1, j)
deltaAllDay += 11
else:
i = 2011
for j in range(m, 11):
md = month2day(i, j)
deltaAllDay += md
deltaAllDay -= d
deltaAllDay += 11
deltaAllDay = -deltaAllDay
# elif y < 2011 or (y == 2011 and m < 11) or (y == 2011 and m == 11 and d < 11):
elif y > 2011 or (y == 2011 and m > 11) or (y == 2011 and m == 11 and d > 11):
if dy != ():
print(dy)
for i in dy:
if i == dy[0]:
for j in range(11, 13):
deltaAllDay += month2day(i, j)
deltaAllDay -= 11
else:
deltaAllDay += ifRunNian(i)
for j in range(1, m):
deltaAllDay += month2day(int(i)+1, j)
deltaAllDay += d
else:
i = 2011
for j in range(11, m):
md = month2day(i, j)
deltaAllDay += md
deltaAllDay -= 11
deltaAllDay += d
return deltaAllDay
def day2week(deltaAllDay):
if deltaAllDay < 0:
deltaAllDay = - deltaAllDay
e = 5- deltaAllDay % 7
print(e)
while e <= 0:
e += 7
while e > 7:
e -= 7
print(f'星期{e}')
else:
e = 5 + deltaAllDay % 7
while e <= 0:
e += 7
while e > 7:
e -= 7
print(f'星期{e}')
print(a)
ymd = a.split(" ")
y, m, d = (ymd[0], ymd[1], ymd[2])
print(f'{y}, {m}, {d}')
# day2week(y, m, d)
k = getDeltaDay(1949, 10, 1)
print(k)
day2week(k)
print(month2day(2012, 2))
全部评论
相关推荐
ziFeng_jiang:没有自知之明,还优中选优上了,单位是有多好?别人优秀的你发了offer别人就会来?别到时候offer全被鸽了又要重新招聘,什么样的公司招什么样的认,不要想着低工资能招个多优秀的人
点赞 评论 收藏
分享