蓝桥杯python练习

Posted on 2022-04-05  30 Views


蓝桥杯快开始了,临阵小磨个枪

洛谷 P1308 [NOIP2011 普及组] 统计单词数

题目链接:
https://www.luogu.com.cn/problem/P1308

python题解:
思路很简单

  • 首先将输入字符串统一转换为小写,方便后续处理
  • 然后将长字符串分割存储为列表形式,便于统计
  • 使用count方法统计个数
  • 如果沟通不为0则继续统计,同时处理空格位置
t = input().lower()
s = input().lower()
l = s.split()
count = l.count(t)
if t not in l:
    print(-1)
else:
    print(count,end = " ")
    length = 0
    for i in l:
        while s[length] == " ":
            length += 1
        if i == t:
            print(length)
            break
        length += len(i)

蓝桥杯 第十一届 回文日期

直接遍历日期会超时,所以遍历年份,然后特判月份小于年份的情况即可

def judge(yy,mm,dd):#判断日期合法性
    yy,mm,dd = map(int,(yy,mm,dd))
    if int(mm) > 12 or int(mm) <= 0:
        return False
    if int(dd) > 31 and mm in [1,3,5,7,8,10,12]:
        return False
    if int(dd) > 30 and mm not in [1,3,5,7,8,10,12]:
        return False
    if (yy % 4 == 0 and yy % 100 != 0) or (yy % 400 == 0):
        if mm == 2 and dd > 29:
            return False
    else:
        if mm == 2 and dd > 28:
            return False
    return True
n = input()
flag1 = 0
flag2 = 0
nn = n[4:]
m = n[:4]
if int(nn[::-1]) < int(m):#特判,防止20200101时直接跳过本年
    temp = int(m)
else:
    temp = int(m)+1
for i in range(temp,10000):
    p = str(i)
    q = p[::-1]
    if flag1!= 1:
        if judge(p,q[:2],q[2:]):
            flag1 = 1
            print(p,q,sep="")
    if p[:2] == p[2:] and flag2 != 1:
        if judge(p,q[:2],q[2:]):
            flag2 = 1
            print(p,q,sep="")
    if flag1 == 1 and flag2 ==1:
        break

欢迎来到parafish的个人博客,这里是一个正在努力的ctfer

路虽远,行则必至