腾讯9.5机考个人代码
1.
import math w = [1,2,3,4,5,6,7] w.sort() def check(num, w): if num > w[-1]**w[-1]&nbs***bsp;num < w[0]**w[0]: return -1, -1 if num in w and 1 in w: return num,1 n = len(w) for i in range(n): if w[i] == 1: continue else: x = math.log(num) / math.log(w[i]) if int(x) in w and w[i]**int(x) == num: return w[i],int(x) else: continue return -1,-1 # print(check(15625, w)) A = [1,8,9, 15625, 512] for m in A: print(check(m, w))
2.
3.
grid = [['b','b','b','b'],['b','b','b','b'],['b','b','b','b'],['b','b','r','b']] n = len(grid[0]) m = len(grid) x,y = 3,2 dirs = [[-1,-2],[-2,-1],[1,-2],[2,-1],[1,2],[2,1],[-1,2],[-2,1]] def dfs(grid,i,j,color,visited,path): if not i in range(m)&nbs***bsp;not j in range(n)&nbs***bsp;grid[i][j] == color&nbs***bsp;visited[i][j]: return path.append([i,j]) visited[i][j] = True for dx,dy in dirs: x = i+dx y = j+dy dfs(grid,x,y,grid[i][j],visited,path) # return path[:] if grid[x][y] == 'b': color = 'r' else: color = 'b' visited = [[False]*n for _ in range(m)] path = [] dfs(grid,x,y,color,visited,path) print(len(path)) # print(path)