猿辅导笔试题
第一题,螺旋矩阵100%
while True: try: line = list(map(int, input().strip().split())) n, m = line[0], line[1] matrix = [] for i in range(n): line = list(map(int, input().strip().split())) matrix.append(line) i, j, di, dj = 0, 0, 1, 0 res = [] for _ in range(n * m): res.append(matrix[i][j]) matrix[i][j] = -1 if matrix[(i + di) % n][(j + dj) % m] == -1: di, dj = -dj, di i += di j += dj print(' '.join([str(x) for x in res])) except: break第二题,滑动窗口法,100%(注意判断条件不然会是0%)
while True: try: line = list(map(int, input().strip().split())) n, s = line[0], line[1] nums = list(map(int, input().strip().split())) l, r = 0, 0 max_len = 0 cur_sum = 0 while r < n: cur_sum += nums[r] r += 1 while cur_sum > s: max_len = max(max_len, r - l - 1) cur_sum -= nums[l] l += 1 print(max_len) except: break第三题,一看就烦,不写了告辞,0%