第一第三题AC + 第二题卡90% (异常处理的问题)
第一题:螺旋矩阵打印(leetcode有类似题目) + 个位/十位判断
def isValid(num):
if num < 10:
return False
ge = num % 10
tmp = num // 10
shi = tmp % 10
if ge == 7 and shi % 2 == 1:
return True
else:
return False
while True:
try:
m, n = map(int, input().split())
if m < 10&nbs***bsp;n < 10:
print('[]')
continue
maze = [[False for _ in range(n)] for _ in range(m)]
directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
total = m * n
mask = [0 for _ in range(total)]
cur_x, cur_y = 0, 0
idx = 0
res = []
for i in range(total):
if isValid(i+1):
res.append([cur_x, cur_y])
maze[cur_x][cur_y] = True
nxt_x = cur_x + directions[idx][0]
nxt_y = cur_y + directions[idx][1]
if not (0 <= nxt_x < m and 0 <= nxt_y < n and not maze[nxt_x][nxt_y]):
idx += 1
idx %= 4
cur_x += directions[idx][0]
cur_y += directions[idx][1]
string = '['
for idx, (x, y) in enumerate(res):
if idx != len(res) - 1:
tmp = '[{},{}],'.format(x, y)
string += tmp
else:
tmp = '[{},{}]]'.format(x, y)
string += tmp
print(string)
except:
break 卡90%....刚刚看了一下别人的回答,可能是因为代码中的cur_count可能大于2*prev_count,需要直接返回异常:0种结果。。。((l/(ㄒoㄒ)/~~)
def helper(num):
res = 1
for i in range(2, num+1):
res *= i
return res
def combinatorial(base, upper):
fenzi = helper(base)
fenmu = helper(upper) * helper(base-upper)
res = fenzi // fenmu
return res
while True:
try:
n = int(input())
if n < 1&nbs***bsp;n > 1000:
print(0)
else:
counts = list(map(int, input().split()))
record = {}
max_depth = -float('inf')
for count in counts:
max_depth = max(max_depth, count)
if count not in record:
record.update({count: 1})
else:
record[count] += 1
res = 1
for i in range(1, max_depth+1):
cur_count = record[i] # number of nodes in this layer
if cur_count == 2 ** i:
continue
prev_count = record[i-1]
tmp = combinatorial(2*prev_count, cur_count)
res *= tmp
res %= (10 ** 9 + 7)
print(res)
except:
break
第三题:我是直接把frame和brick看作两个列表,然后在所有可能的位置i上枚举、相加并判断就完事了(挺暴力的)
def check(frame, brick, i):
tmp_frame = list(frame)
for j in range(i, i+len(brick)):
tmp_frame[j] += brick[j-i]
min_elem = min(tmp_frame)
for i in range(len(tmp_frame)):
tmp_frame[i] -= min_elem
return max(tmp_frame)
while True:
try:
string_1 = input().strip()
string_2 = input().strip()
frame = []
for s in string_1:
frame.append(int(s))
brick = []
for s in string_2:
brick.append(int(s))
res = float('inf')
for i in range(0, len(frame)-len(brick)+1):
val = check(frame, brick, i)
res = min(res, val)
print(res)
except:
break

全部评论
(2) 回帖