数学规律 | HJ53 杨辉三角的变形
# 最优解(找规律) import sys alt = [2, 3, 2, 4] # 发现规律,从第三行开始2324循环 for line in sys.stdin: n = int(line.strip()) if n < 3: print(-1) if n >= 3: print(alt[(n - 3) % 4]) # 所以对4求余,映射到上面alt列表中 # 我的代码(10000行时超内存限制) n = int(input()) M = [[0]*n for _ in range(n)] pos = -1 for i in range(n): for j in range(n-i-1, n): if j == n-1-i: M[i][j] = 1 continue elif j != n-1: M[i][j] = sum(M[i-1][j-1:j+2]) else: M[i][j] = M[i-1][j-1]*2 + M[i-1][j] if i==n-1 and pos == -1 and M[i][j]%2==0: pos = j+1 print(pos)
用时:25min
华为笔试刷题 文章被收录于专栏
高质量题: 1~40:HJ16,HJ22,HJ24,HJ26,HJ27,HJ28,HJ35,HJ37,HJ39; 40~80:HJ41,HJ42,HJ43,HJ44,HJ48,HJ50,HJ52,HJ53,HJ57,HJ61,HJ63,HJ64,HJ70,HJ71,HJ74,HJ77; 80~108:HJ82,HJ85,HJ88,HJ89,HJ93,HJ95,HJ98,HJ103,HJ107