【思特奇杯·云上蓝桥-算法训练营】第1周:日期文问题

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。

比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。

给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

输入

一个日期,格式是"AA/BB/CC"。 (0 <= A, B, C <= 9)

输出

输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。

样例输入

02/03/04

样例输出

2002-03-04

2004-02-03

2004-03-02

分析:我的思路,首先利用replace()函数,将'/'去掉,再利用spilt()函数将字符串变成列表,if判断大于或小于60,从而判断年份(但是发现replace()  split()方法不可行)

正确代码:

n = input()
a = []
m = {1:31, 2:30, 3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
a.append(n[:2])
a.append(n[3:5])
a.append(n[6:])
res = []
if int(a[0]) in range(60):
    res.append(['20'+a[0]])
    res[0].append(a[1])
    res[0].append(a[2])
    if int(a[2]) > m[int(a[1])] or int(a[1]) > 12:
        res[-1] = []
else:

    res.append(['19'+a[0]])
    res[0].append(a[1])
    res[0].append(a[2])
    if int(a[2]) > m[int(a[1])] or int(a[1]) > 12:
        res[-1] = []
if int(a[2]) in range(60):
    res.append(['20'+a[2]])
    res[1].append(a[1])
    res[1].append(a[0])
    if int(a[0]) > m[int(a[1])] or int(a[1]) > 12:
        res[-1] = []
else:

    res.append(['19'+a[2]])
    res[1].append(a[1])
    res[1].append(a[0])
    if int(a[0]) > m[int(a[1])] or int(a[1]) > 12:
        res[-1] = []
if int(a[2]) in range(60):

    res.append(['20'+a[2]])
    res[2].append(a[0])
    res[2].append(a[1])
    if int(a[0]) > 12 or int(a[1]) > m[int(a[0])]:
        res[-1] = []
else:

    res.append(['19'+a[2]])
    res[2].append(a[0])
    res[2].append(a[1])
    if int(a[0]) > 12 or int(a[1]) > m[int(a[0])]:
        res[-1] = []
a = []
print(res)
for i in range(3):
    if res[i] != []:
        a.append('-'.join(res[i]))
print(a)

(来自python蓝桥杯讲解)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值