8.13bilibili笔试
第一题
A = input() B = input() hashA = {} hashB = {} for ch in A: if ch in hashA: hashA[ch] += 1 else: hashA[ch] = 1 for ch in B: if ch in hashB: hashB[ch] += 1 else: hashB[ch] = 1 if len(hashA) != len(hashB): print(0) else: for key in hashA.keys(): if key not in hashB: print(0) break elif hashA[key] != hashB[key]: print(0) break else: print(1)
第二题:
import math n = int(input()) def issu(num): if num < 2: return False for i in range(2, int(math.sqrt(num)) + 1): if num % i == 0: return False return True tmp = [] for i in range(10001): if issu(i): tmp.append(i) i, j = 0, 0 ans = 0 while j < len(tmp) and i < len(tmp): subsum = sum(tmp[i:j + 1]) if subsum == n: ans += 1 i += 1 j = i elif subsum < n: j += 1 else: i += 1 print(ans)
第三题
#######0-1背包问题########## n,m=[int(i) for i in input().split()] mi,ri=[],[] for i in range(n): a,b=[int(i) for i in input().split()] mi.append(a) ri.append(b) dp=[[0 for _ in range(m+1)] for _ in range(n+1)] for i in range(1,n+1): for j in range(1,m+1): if mi[i-1]>j: dp[i][j]=dp[i-1][j] else: dp[i][j]=max(dp[i-1][j-mi[i-1]]+ri[i-1],dp[i-1][j]) print(dp[n][m])