1、迷宫
# 迷宫
def solve(grid):
m, n = len(grid), len(grid[0])
route = [(0, 0)]
grid[0][0] = 1 # 1为不可通行 0为可通行
dfs(0, 0, grid, route)
def dfs(i, j, grid, route):
m, n = len(grid), len(grid[0])
if i == m-1 and j == n-1:
print(route)
for x, y in [(i-1,j), (i+1, j), (i,j-1), (i, j+1)]:
if x >= 0 and x < m and y >= 0 and y < n and grid[x][y] == 0:
grid[x][y] = 1
route.append((x, y))
dfs(x, y, grid, route)
grid[x][y] = 0
route.pop()
2、MP3位置
n = int(input())
s = input()
l = []
for i in range(1, n+1):
l.append(i)
f = 1 # 当前页面第一首歌索引位置
p = 1 # 当前页面光标所在位置
if n < 5:
for i in s:
if i == 'U':
if p == 1:
p = n
else:
p -= 1
if i == 'D':
if p == n:
p = 1
else:
p += 1
else:
for i in s:
if i == 'U':
if f == 1 and p == 1:
f = n - 3
p= 4
elif p == 1:
f -= 1
else:
p -= 1
if i == 'D':
if f == n-3 and p == 4:
f = 1
p = 1
elif p == 4:
f += 1
else:
p += 1
for i in l[f-1:f-1+4]:
print(i, end=' ')
print()
print(f+p-1)
3、最长公共子串【有多个时输出较短字符串的第一个】
def solve(nums1, nums2):
m, n = len(nums1), len(nums2)
if m >= n:
nums1, nums2 = nums2, nums1
m, n = len(nums1), len(nums2)
dp = [[0] * (n+1) for i in range(m+1)]
start = 0
max_len = 0
for i in range(m-1, -1, -1):
for j in range(n-1, -1, -1):
if nums1[i] == nums2[j]:
dp[i][j] = dp[i+1][j+1] + 1
else:
dp[i][j] = 0
if dp[i][j] >= max_len:
max_len = dp[i][j]
start = i
res = nums1[start:start+max_len]
return res
s1 = input()
s2 = input()
print(solve(s1, s2))
4、配置文件恢复
while True:
s = input().split()
dic = {"reset":"reset what", "reset board":"board fault", "board add":"where to add",
"board delete":"no board at all", "reboot backplane":"impossible", "backplane abort":"install first"}
keys = list(dic.keys())
if len(s) == 0 or len(s) > 2:
print("unknown command")
elif len(s) == 1:
count = 0
target_key = ''
keys_split = [key.split() for key in keys]
keys_pool = [each for each in keys_split if len(each) == 1]
# print(keys_pool)
for each in keys_pool:
# print(s[0], each)
# if s[0] in each[0]:
if each[0].startswith(s[0]):
count += 1
target_key = each[0]
if count > 1 or count == 0:
print('unknown command')
else:
print(dic[target_key])
else:
keys_split = [key.split() for key in keys]
keys_pool = [each for each in keys_split if len(each) == 2]
count = 0
target_key = ''
for each in keys_pool:
# print(s, each)
if each[0].startswith(s[0]) and each[1].startswith(s[1]):
count += 1
target_key = each[0] + ' ' + each[1]
if count > 1 or count == 0:
print('unknown command')
else:
print(dic[target_key])
# break
5、24点
def solve(nums):
target = 24
eps = 1e-6
add, minus, multi, divide = 0, 1, 2, 3
def back(nums):
if not nums:
return 'false'
if len(nums) == 1:
if abs(nums[0]-target) < eps:
return 'true'
else:
return 'false'
for i, x in enumerate(nums):
for j, y in enumerate(nums):
if i != j:
nums1 = []
for k, z in enumerate(nums):
if k != i and k != j:
nums1.append(z)
for k in range(4):
if (k == 0 or k == 2) and i > j:
continue # + *符合交换律
if k == add:
nums1.append(x+y)
elif k == minus:
nums1.append(x-y)
elif k == multi:
nums1.append(x*y)
elif k == divide:
if abs(y) < eps:
continue # 排除除数为0
else:
nums1.append(x/y)
if back(nums1) == 'true':
return 'true'
nums1.pop()
return 'false'
return back(nums)
s = list(map(int, input().split()))
print(solve(s))
6、矩阵乘法
x = int(input())
y = int(input())
z = int(input())
w1 = []
for i in range(x):
cur = list(map(int, input().split()))
w1.append(cur)
w2 = []
for i in range(y):
cur = list(map(int, input().split()))
w2.append(cur)
def solve(i, j, w1, w2):
a = w1[i]
b = [each[j] for each in w2]
res = 0
for i in range(len(a)):
res += a[i] * b[i]
return res
res = []
for i in range(x):
tmp = []
for j in range(z):
cur = solve(i, j, w1, w2)
tmp.append(cur)
res.append(tmp)
for i in res:
for j in i:
print(j, end=' ')
print(' ')
7、矩阵乘法运算量计算
n = int(input())
dic = {}
for i in range(n):
key = chr(ord('A')+i)
dic[key] = list(map(int, input().split()))
x = input()
stack = []
num = 0
# stack
for i in range(len(x)):
if x[i] != ')':
stack.append(x[i])
else:
c, b = stack.pop(), stack.pop()
stack.pop()
num += dic[b][0] * dic[c][1] * dic[b][1]
dic[b] = [dic[b][0], dic[c][1]]
stack.append(b)
print(num)
8、通配符匹配
def solve(s, p):
m, n = len(s), len(p)
dp = [[0] * (n+1) for i in range(m+1)]
dp[0][0] = 1
for i in range(1, n+1):
if p[i-1] == '*':
dp[0][i] = 1
else:
break
for i in range(1, m+1):
for j in range(1, n+1):
if p[j-1] == '*':
dp[i][j] = dp[i][j-1] | dp[i-1][j]
elif (p[j-1] == '?' and s[i-1].isalnum()) or s[i-1].lower() == p[j-1].lower():
dp[i][j] = dp[i-1][j-1]
return dp[m][n]
p = input()
s = input()
res = solve(s, p)
if res == 1:
print('true')
else:
print('false')
9、参数解析
m = input().split()
result = []
count = 0
i = 0
while i < len(m):
if m[i].count('"') == 2:
result.append(m[i].replace('"', ''))
count += 1
i += 1
elif m[i].count('"') == 1:
# j = 0
tmp = m[i].replace('"', '')
for j in range(i+1, len(m)):
if m[j].count('"') == 1:
# count += 1
tmp += ' '
tmp += m[j].replace('"', '')
break
else:
tmp = tmp + ' ' +m[j].replace('"', '')
result.append(tmp)
count += 1
i = j + 1
else:
result.append(m[i].replace('"', ''))
count += 1
i += 1
print(count)
for each in result:
print(each)
10、火车进站
def solve(nums):
res = []
def back(in_lst, path, out_lst, res):
if len(in_lst) == 0 and len(path) == 0:
res.append(out_lst)
if len(in_lst) > 0:
back(in_lst[1:], path+[in_lst[0]], out_lst, res)
if len(path) > 0:
back(in_lst, path[:-1], out_lst+[path[-1]], res)
return res
back(nums, [], [], res)
return res
n = int(input())
s = list(map(int, input().split()))
res = solve(s)
res1 = []
for each in res:
cur = list(map(str, each))
res1.append(' '.join(cur))
res2 = sorted(res1)
for each in res2:
print(each)
11、真分数分解为埃及分数
while True:
try:
a, b = list(map(int, input().split('/')))
a *= 10
b *= 10
res = []
while a:
for i in range(a, 0, -1):
if b % i == 0:
cur = '1/'+str(int(b/i))
res.append(cur)
a -= i
break
print('+'.join(res))
except:
break
12、合法IP
flag = 'YES'
try:
n = input().split('.')
except:
flag = 'NO'
n = []
if len(n) != 4:
flag = 'NO'
# print(n)
for each in n:
if not each.isdigit():
flag = 'NO'
break
try:
if int(each) > 255 or (each.startswith('0') and len(each) > 1):
flag = 'NO'
break
except:
flag = 'NO'
print(flag)
13、连续最长的数字串
while True:
try:
s = input()
for i in range(len(s)):
if not s[i].isdigit():
s = s.replace(s[i], ' ')
s1 = s.split()
max_len = 0
for each in s1:
if len(each) > max_len:
max_len = len(each)
res = []
for each in s1:
if len(each) == max_len:
res.append(each)
out = ''.join(res) + ',' + str(max_len)
print(out)
except:
break
14、Redraiment的走法
def solve(s):
n = len(s)
dp = [1] * n
res = 0
for i in range(n):
for j in range(i):
if s[i] > s[j]:
dp[i] = max(dp[i], dp[j]+1)
if dp[i] > res:
res = dp[i]
return res
s = input()
nums = list(map(int, input().split()))
print(solve(nums))
15、求解立方根
def solve(n):
eps = 1e-6
low = min(-1.0, n)
high = max(1, n)
ans = (low + high) / 2
while abs((ans * ans * ans - n)) > eps:
if ans * ans * ans < n:
low = ans
else:
high = ans
ans = (low + high) / 2
return round(ans, 1)
s = float(input())
print(solve(s))
## 20230502