题目参考:8.22 腾讯笔试情况统计+题目+解题思路+AK代码
1. 开锁
思路:对于每一个锁的开锁时间排序,list_j = [a_0j a_1j ... a_nj]. 从时间最短的开始试,每一个打开的概率是一样的(1/n), 到第i个打开的时间是前i个数之和(sum(list_j[:i+1])),期望就是概率*时间相加嘛。
n, m = map(int, input().split()) A = [] for i in range(n): row = list(map(int, input().split())) A.append(row) time = 0.0 for j in range(m): lists = [] for i in range(n): lists.append(A[i][j]) lists.sort() expect = 0 for i in range(n): expect += (1.0/n)*sum(lists[:i+1]) time += expect print(time)测试例子
2 4 347 177 40 84 107 282 347 193
2. 勇闯币圈
这题思路很简单嘛,就是更新新的状态列Pi = [p1 p2 p3]. 更新的公式就等于 p_i = matrix[i] .* Pi (element-wise multiplication), 步长多长就更新几次。
t = int(input()) for i in range(t): step = int(input()) pie = list(input().split()) matrix = [] for j in range(3): row = list(input().split()) matrix.append(row) k = 0 while k < step: k+=1 new = [] for j in range(3): row = matrix[j] cur = 0 for l in range(3): cur += float(row[l]) * float(pie[l]) new.append(cur) pie = new # print(pie) if pie[2] > 0.5: print(1) else: print(0)这个没有记测试例子
3. 迎宾车队
思路就是先排序,然后遍历每一个节点,往前走看能走到的最大步长j是多少,再每次取最大值。
n = int(input()) nums = list(map(int, input().split())) nums.sort() cur = 1 for i in range(1,n): j = cur while i-j >= 0 and nums[i] - nums[i-j] <= 10: j+=1 cur = max(cur,j) print(cur)测试例子
6 8 3 17 24 21 44
4. 水站的水流量
n, t = map(int, input().split()) if t >= int((n*(n+1))/2): print(int((n*(n+1))/2)) else: dp = [0]*(n+1) for i in range(1,n+1): dp[i] = int((i*(i+1))/2) i = 0 while dp[i] < t: i+=1 if dp[i] == t: print(dp[i]) elif dp[i] > t: remain = t - dp[i-1] if remain >= i-1: print(dp[i-1]+i-2) else: print(dp[i-1])测试例子
3 5
这道题只过了40%, 想破头也不知道哪里不对。
5. 定点轰炸
这道题跟力扣130一样一样的,解法DFS.
n = int(input()) grid = [] for i in range(n): row = list(map(int, input().split())) grid.append(row) def dfs (grid, i, j): if not i in range(n)&nbs***bsp;not j in range(n)&nbs***bsp;grid[i][j] != 0: return grid[i][j] = -1 dfs(grid,i-1,j) dfs(grid,i+1,j) dfs(grid,i,j-1) dfs(grid,i,j+1) for i in range(n): dfs(grid, i, 0) dfs(grid, i, n-1) for j in range(n): dfs(grid, 0, j) dfs(grid, n-1, j) for i in range(n): for j in range(n): if grid[i][j] == -1: grid[i][j] = 0 elif grid[i][j] == 0: grid[i][j] = 2 for i in range(n): for j in range(n): print(grid[i][j], end =' ') print('')测试例子有点长也不记得了。
通常在题里面我们直接返回grid,但是要注意输出模式。要根据他的案例的输出样式来调整,不然就是0%。
全部评论
(1) 回帖