3.21 最右笔试
1.最大连续子序和,但要记录起始位置
if __name__ == '__main__': ary = list(map(float, input().split())) dp = [0] * len(ary) dp[0] = ary[0] start = [0] for i in range(1, len(ary)): if dp[i-1] + ary[i] >= ary[i]: dp[i] = dp[i-1] + ary[i] else: dp[i] = ary[i] start.append(i) res = max(dp) print(start[-1], dp.index(res)+1, res)2.机器人走格子,dfs
def count_sum(i, j): res = 0 for i in str(i): res += int(i) for i in str(j): res += int(j) return res def get_neigh(i, j, m, n): neighs = [] if i < n-1: neighs.append([i+1, j]) if i > 0: neighs.append([i-1, j]) if j < m-1: neighs.append([i, j+1]) if j > 0: neighs.append([i, j-1]) return neighs def dfs(p, i, j, k, res, m, n): for ne in get_neigh(i, j, m, n): if p[ne[0]][ne[1]] and count_sum(ne[0], ne[1]) <= k: p[ne[0]][ne[1]] = False res[0] += 1 dfs(p, ne[0], ne[1], k, res, m, n) if __name__ == '__main__': k, m, n = tuple(map(int, input().split())) p = [[True for _ in range(m)] for _ in range(n)] res = [0] for i in range(n): for j in range(m): if p[i][j] and count_sum(i, j) <= k: p[i][j] = False res[0] += 1 dfs(p, i, j, k, res, m, n) print(res[0])