1、回文日期
思路:构造回文和ABABBABA,通过判断数字是否符合要求确定
回文日期:可以这样理解,8位数字,前四个与后四个的逆置是相同的。要求下一个回文日期,通过8位数字开始遍历,工作量太大,可以简化为从前四位数开始遍历到10000,(反正后四位是前四位的转置)但是,要考虑到日期的合法性,所以当遍历到的数字符合回文要求时,还需要检查它是否符合日期要求。
ABABBABA:time有两个要求:①time[0:2]==time[2:4] ②得到的数字要符合日期要求
日期要求:后四位为前四位的转置设为temp,显然month=temp[0:2],day=temp[2:4]。
根据常识,月份在1,3,5,7,8,10,12为31天;在4,6,9,11为30天;在2月时,则需要判断闰年:是则29天,否则28天。
可以写一个判断日期的函数,应为判断回文日期和ABABBABA都有需要用到,减少代码冗余。但是这个代码可能还有没考虑到的地方,只有90分。
def istime(month,day):
year=int((month+day)[::-1])
day=int(day)
if month in ['01','03','05','07','08','10','12']:
if 1<=day<=31:
return True
else:
return False
elif month=='02':
if (year%4==0 and year%100!=0) or (year%400==0):
if 1<=day<=29:
return True
else:
return False
else:
if 1<=day<=28:
return True
else:
return False
elif month in ['04','06','09','11']:
if 1<=day<=30:
return True
else:
return False
else:
return False
#80分
time=int(input()[0:4])
#回文日期
for i in range(time+1,10000):
i=str(i)
temp=i[::-1]
month=temp[0:2]
day=temp[2:4]
if istime(month,day):
if int(i)>time:
print("{}{}".format(i,i[::-1]))
break
else:
continue
else:
continue
#ABABBABA
for i in range(time+1,10000):
i=str(i)
temp=i[::-1]
month=temp[0:2]
day=temp[2:4]
if len(set(i)) == 2 and i[0]==i[2] and i[1]==i[3]:
if istime(month,day):
if int(i)>time:
print("{}{}".format(i,i[::-1]))
break
else:
continue
else:
continue
下面是我一个朋友写的,他是同时进行回文日期和ABABBABA判断,大概思路也差不多(100分)
s = input().strip()
y = int(s[:4])
a = 0
b = 0
aflag = 0
bflag = 0
while True:
temp = str(y)[::-1]
m = int(temp[:2])
d = int(temp[2:4])
if 1<= m <= 12:
if m == 2:
if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0):
if 1<= d <=29:
if not aflag:
a = str(y)+temp
if int(a) > int(s):
aflag = 1
if not bflag:
if len(set(list(temp))) == 2 and temp[0] == temp[2] and temp[1] == temp[3]:
b = str(y)+temp
if int(b) > int(s):
bflag = 1
else:
if 1 <= d <= 28:
if not aflag:
a = str(y)+temp
if int(a) > int(s):
aflag = 1
if not bflag:
if len(set(list(temp))) == 2 and temp[0] == temp[2] and temp[1] == temp[3]:
b = str(y)+temp
if int(b) > int(s):
bflag = 1
elif m in (4,6,9,11):
if 1 <= d <= 30:
if not aflag:
a = str(y)+temp
if int(a) > int(s):
aflag = 1
if not bflag:
if len(set(list(temp))) == 2 and temp[0] == temp[2] and temp[1] == temp[3]:
b = str(y)+temp
if int(b) > int(s):
bflag = 1
else:
if 1 <= d <= 31:
if not aflag:
a = str(y)+temp
if int(a) > int(s):
aflag = 1
if not bflag:
if len(set(list(temp))) == 2 and temp[0] == temp[2] and temp[1] == temp[3]:
b = str(y)+temp
if int(b) > int(s):
bflag = 1
if aflag and bflag:
print(a)
print(b)
break
y = y + 1
2、成绩分析
思路:题很简单,只需要设置一个列表,每输入一个数就添加到列表中。利用python的内置函数max,min,sum求得成绩的最大值,最小值,总和。计算平均数时,需要四舍五入保留正好两位小数。直接用round()函数,设置第二个参数为2,保留小数的位数。
但是对于保留后第二位小数为0的情况,不会输出小数点后第二位。可以设置输出格式{:.2f}不会出错。(100分)
N=int(input())
num=[]
for i in range(N):
num.append(int(input()))
mean=round(sum(num)/N,2)
print(max(num))
print(min(num))
print("{:.2f}".format(mean))
3、字串排序
不会。。。
4、平面切分
思路:是在网上看别人写的,这是链接:python蓝桥杯 平面切分_衍ちゃん的博客-CSDN博客
n=int(input())
l=[]
for i in range(n):
a,b=list(map(int,input().split()))
l.append((a,b))
l=list(set(l))
def getnode(l1,l2):
a1,b1=l1[0],l1[1]
a2,b2=l2[0],l2[1]
if(a1==a2):
return False
else:
x=(b2-b1)/(a1-a2)
y=a1*x+b1
x = round(x, 10)
y = round(y, 10) #这两句,表示最多取十位小数
return (x,y)
ans=len(l)+1
for i in range(1,len(l)):
node=[]
for j in range(i):
# print(i,j)
if(getnode(l[j],l[i])):
(x,y)=getnode(l[j],l[i])
node.append((x,y))
else:
continue
# print(ans)
node=list(set(node))
# if(len(node)!=0):
ans+=len(node)
print(ans)
5、子串分值和
思路:编写一个函数,传入字符串,将字符串去重(先转换为集合,再转换为列表),返回列表长度。利用两个for循环,循环遍历,截取字符串,累加字符串(去重后)的长度。但是只有40分,暴力肯定超时啦
def different(s):
s=list(set(s))
return len(s)
s=input()
l=len(s)
sum=0
for i in range(l):
for j in range(i+1,l+1):
temp=s[i:j]
c=different(temp)
sum+=c
print(sum)
后来在网上查到一篇(100分),链接:【蓝桥杯VIP】试题 历届试题 子串分值和(满分 Python解法+Java解法)_怠惰の梦歌的博客-CSDN博客_子串分值和python
思路:
当前位置与末位置的差+1 因为: 该元素在当前位置以及之后的位置无论之后还有没有该元素,该元素都是只提供一次.
当前位置与该元素上一次出现位置的差 因为: 该元素在当前位置 以及在 该元素上一次出现的地方消失后的位置之间都要提供次数.
list1=list(input())
list2=[-1 for i in range(26)]
count=0
for i in range(len(list1)):
index=ord(list1[i])-ord('a')
count+=(len(list1)-i)*(i-list2[index])
list2[index]=i
print(count)
大家对于以上题目有更好的想法,欢迎分享。
对于本文有写得不对的地方欢迎指正。
一起学习,共同进步!