暴力哈
count=0
for i in range(1,2021):
count+=(str(i).count(“2”))
print(count)
文件内容
暴力吧,比第十届有个要简单一些
with open("E:\蓝桥杯\Python\真题\十一届\\t.txt") as f :
num = list(f.readlines())
hang = len(num)
lie = len(num[-1])#多了一个换行符
for i in range(hang):
if i != hang-1:
num[i]=num[i][:-1]
count=0
#hang
for i in range(hang):
for j in range(lie-3):
if num[i][j:j+4]=="2020":
count+=1
#lie
for i in range(lie):
for j in range(hang-3):
s=""
for m in range(j,j+4):
s+=num[m][i]
if s=="2020":
count+=1
#xie
for i in range(hang-3):
for j in range(lie-3):
s=""
for m in range(0,4):
s+=num[i+m][j+m]
if s=="2020":
count+=1
print(count)
一开始想计算月份和星期一重叠的个数,然后用全部天数+月份数+星期一个数-重叠数
感觉自己数学老是对细节不太行,就是感觉容易多算一天少算
但是这还是不停调试的结果,在比赛这种容易错,还废时间
li=[3,0,3,2,3,2,3,3,2,3,2,3]
run=[3,1,3,2,3,2,3,3,2,3,2,3]
##time=[[0 for _ in range(12)] for j in range(21)]
##time[0][0]=6
##chonghe=0
##for i in range(21):
## if ((i+2000)%100==0) and (i+2000)%400==0 or((i+2000)%100!=0 and (i+2000)%4==0):
## yue = run
## else:
## yue = li
## for j in range(12):
## if j ==0 and i !=0:
## time[i][0]=(time[i-1][11]+yue[j])%7
## if i == 0 and j == 0:
## continue
## elif j ==0 and i !=0:
## time[i][0]=(time[i-1][11]+yue[j])%7
## else:
## time[i][j]=(time[i][j-1]+yue[j])%7
## chonghe+=time[i].count(1)
##print(chonghe)
li=[31,28,31,30,31,30,31,31,30,31,30,31]
run=[31,29,31,30,31,30,31,31,30,31,30,31]
count=0
d = 6
flag = 0
for i in range(21):
for j in range(12):
if ((i+2000)%100==0) and (i+2000)%400==0 or((i+2000)%100!=0 and (i+2000)%4==0):
yue = run
else:
yue = li
for m in range(yue[j]):
if d == 1 or m==0 :
count+=2
else:
count+=1
if i == 20 and j == 9 and m == 0:
flag = 1
break
d = (d+1)%7
## print(i,j,m,d,count)
if flag == 1:
break
if flag == 1:
break
print(count)
num=[[0 for i in range(39)] for j in range(39)]
num[0][0]=1
ge=1
for i in range(1,39):
if i%2!=0:
num[0][i]=num[0][i-1]+1
else:
num[0][i]=num[0][i-1]+4*ge
ge+=1
print(num[0][-1]-19)
最短应该是abcde倒着排序,冒泡排序次数为:n*(n-1)/2,最接近的是15,但是比200多了10次,然后交换一个到最前面,就是l
count=200
s=list("onmlkjihgfedcba")
def jisu(li):
print(li)
count=0
for i in range(15):
for j in range(15-i-1):
if str(li[j])>str(li[j+1]):
li[j+1],li[j]=li[j],li[j+1]
count+=1
return count
for i in range(14,1,-1):
li=s[:]
ss=li.pop(i)
li=list(ss+"".join(li))
print(jisu(li))
直接算吧,有个内置函数round()
ge=int(input())
li=[]
for i in range(ge):
li.append(int(input()))
jige,youxiu=0,0
for i in li:
if i>=85:
youxiu+=1
if i>=60:
jige+=1
print(round(jige/ge*100),end="%")
print()
print(round(youxiu/ge*100),end="%")
一个一维数组
li=[0 for i in range(26)]
s = input()
for i in s:
li[ord(i)-ord("a")]+=1
print(chr(li.index(max(li))+97))
print(max(li))
这道题前几天看了,还记得主要是得!观察规律
左右相差不大于1:
行数为偶数:落中间
行数为奇数:落中间左右,取最大就行
==满分答案==不是回溯,是覆盖
#40分
hang = int(input())
num=[]
for i in range(hang):
li=list(map(int,input().split()))
num.append(li)
count=0
path=[]
ans=[]
def dfs(num,i,j):
path.append(num[i][j])
if len(path)==hang :
if path[hang-1]==num[hang-1][hang//2] or(path[-1]==num[hang-1][hang//2-1] and hang%2==0):
ans.append(sum(path))
return
elif len(path)==hang:
return
dfs(num,i+1,j)
path.pop()
dfs(num,i+1,j+1)
path.pop()
dfs(num,0,0)
big=0
print(max(ans))
#满分
hang = int(input())
num=[]
for i in range(hang):
li=list(map(int,input().split()))
num.append(li)
for i in range(1,hang):
for j in range(len(num[i])):
if j == 0:
num[i][j]+=num[i-1][j]
elif j == i:
num[i][j]+=num[i-1][j-1]
else:
num[i][j]+=max(num[i-1][j],num[i-1][j-1])
if hang%2==0:
print(max(num[hang-1][hang//2],num[hang-1][hang//2-1]))
else:
print(num[hang-1][hang//2])
求交点个数,利用焦点个数求,好像有个数学公式
大佬的解答
n= int(input())#线的个数
#存放直线A,B
line=[]
#面的个数
ans=0
#是否为重边,false表示不是
st=[0 for i in range(n+1)]
#创建一个二维数组,存放直线
for i in range(n):
line.append(list(map(int,input().split())))
#交点
point=[]#每次加入新的线,集合重置
for j in range(i):
if st[j]:#是重边
continue
if line[i][0]==line[j][0]:#斜率是否相等
if line[j][1]==line[j][1]:#是否重边
st[i]=True
break
else:
continue#平行线,不需要计算交点
left=(line[j][1]-line[i][1])/(line[i][0]-line[j][0])#交点的x
right=line[i][0]*left+line[i][1]#y
if [left,right] not in point:#去重
point.append([left,right])
if not st[i]:
ans+=len(point)+1
print(ans+1)
看着还行哈,就是在给装备装装饰珠的时候,要记得判断一下小于装备装饰孔的等级之下的装饰珠哪个最大,装个最大的就行了
感觉很麻烦,因为有太多种情况了,不知道从小到大装珠子还是从大到小,考虑动态规划,
----找到一个60分的答案