腾讯笔试 8.23 python
第一题 括号匹配不会
第二题 求积分 这题目***在于需要取小数点6,卡了我半个小时 100
import math def solution(A,B,C,D): ans = (1/3)*A*(D**3-C**3) + 0.5*(D**2-C**2)+B*(D-C) return ans n = int(input()) for _ in range(n): a,b,c,d = list(map(int,input().split())) ans = abs(solution(a,b,c,d)) print("%.6f"%ans)第三题 选队长 n*2**(n-1) 100
n = int(input()) mod = 10 ** 9 + 7 def cal(x, n, p): res = 1 while n > 0: if n % 2: res = res * x % p x = x * x % p n = n//2 return res ans = n * cal(2, n - 1, mod) ans = ans % mod print(ans)第四题 图中配对问题 90
from collections import defaultdict n, m = list(map(int, input().split())) A = [] for i in range(m): A.append(input().split()) dp = defaultdict(list) for x, y in A: dp[x].append(y) dp[y].append(x) # print(dp) mem = defaultdict(list) for k, v in dp.items(): new_k = ''.join(sorted(v)) mem[new_k].append(k) ans = 0 for k, v in mem.items(): n = len(v) if n > 1: ans += n*(n-1)/2 print(int(ans))第五题 没时间做了 赛后做了一下,不知道对不对
from collections import defaultdict A = defaultdict(list) B = defaultdict(list) n,m,k = list(map(int,input().split())) for _ in range(m): x,y,_ = list(map(int,input().split())) A[x].append(y) A[y].append(x) for _ in range(k): x, y = list(map(int, input().split())) B[x].append(y) ans = float('inf') dp = [ans]*(n+1) dp[1] = 0 for i in range(100): for k,v in B.items(): for j in v: dp[j] = min(dp[j], dp[k]) for k, v in A.items(): for j in v: dp[j] = min(dp[j], dp[k] + 1) print(dp[n])