目录
1.金币 357
def f(x):
ans=0
for i in range(1,x+1):
ans+=i*i
return ans
n=int(input())
for x in range(1,n+1):
if int(x*(x+1)//2)==n:
print(int(f(x)))
break
if x*(x+1)/2>n:
m=(x-1)*x/2
print(int(f(x-1)+(n-m)*x))
break
2.优秀的拆分(位运算)
n=int(input())
for i in range(25,-1,-1):
if n>>i&1:
print(1<<i,end=' ')
3.蓝肽子序列(只能得50分,超时了)
a=input()
b=input()
def f(x):
s=[]
th=''
for i in x:
if 'A'<=i<='Z':
if th=='':
th+=i
else:
ph=th
s.append(ph)
th=''
th+=i
else:
th+=i
s.append(th)
return s
n=len(f(a))
m=len(f(b))
dp=[[0 for i in range(1050)]for j in range(1050)]
for i in range(1,n+1):
for j in range(1,m+1):
if f(a)[i-1]==f(b)[j-1]:
dp[i][j]=dp[i-1][j-1]+1
else:
dp[i][j]=max(dp[i-1][j],dp[i][j-1])
print(dp[n][m])
4.穿越雷区(第六届 决赛)
import sys
from queue import *
n=int(input())
map=[]
for i in range(n):
a=input()
a=a.split(' ')
map.append(a)
dir=[(1,0),(-1,0),(0,-1),(0,1)]
vis=[[0 for i in range(105)]for j in range(105)]
for i in range(n):
for j in range(n):
if map[i][j]=='A':
x,y=i,j
if map[i][j]=='B':
e,d=i,j
q=Queue()
q.put((x,y,0,0))
vis[x][y]=1
def bfs():
global e,d
while not q.empty():
now=q.get()
if now[0]==e and now[1]==d:
print(now[3])
sys.exit()
for i in range(4):
new_x=now[0]+dir[i][0]
new_y=now[1]+dir[i][1]
if new_x>=n or new_x<0 or new_y>=n or new_y<0:
continue
if vis[new_x][new_y]==1:
continue
if map[new_x][new_y]==map[now[0]][now[1]]:
continue
vis[new_x][new_y]=1
q.put((new_x,new_y,map[new_x][new_y],now[3]+1))
print(bfs())